none
diskpart / list volume - list disk ilişkisi RRS feed

  • Soru

  • Merhabalar,

    Aşağıdaki şekilde kullanıcıdan seçtirdiğim klasörün ilk harfini alıp diskpart ile volume numarasını öğreniyorum.

    Ancak bana bu volume numarasının hangi diske ait olduğu bilgisi gerekiyor. Disk1 mi, 2 mi, 3 mü...

    Bu konuda nasıl bir yol izleyerek volume numarası ile disk numarasını eşleştirebilirim?

    public int GetIndexOfDrive(string drive)
            {
                drive = drive.Replace(":", "").Replace(@"\", "");
    
                // execute DiskPart programatically
                Process process = new Process();
                process.StartInfo.FileName = "diskpart.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.Start();
                process.StandardInput.WriteLine("list volume");
                process.StandardInput.WriteLine("exit");
                string output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
    
                // extract information from output
                string table = output.Split(new string[] { "DISKPART>" }, StringSplitOptions.None)[1];
                var rows = table.Split(new string[] { "\n" }, StringSplitOptions.None);
                for (int i = 3; i < rows.Length; i++)
                {
                    if (rows[i].Contains("Volume"))
                    {
                        int index = Int32.Parse(rows[i].Split(new string[] { " " }, StringSplitOptions.None)[3]);
                        string label = rows[i].Split(new string[] { " " }, StringSplitOptions.None)[8];
    
                        if (label.Equals(drive))
                        {
                            return index;
                        }
                    }
                }
    Kodu buradan aldım.


    Faruk G&amp;amp;#214;K&amp;amp;#199;E

    15 Ağustos 2019 Perşembe 08:33

Yanıtlar

  • Oldukca kotu bir kodu ornek almissin. Diskpart hem kullanımı tehlikeli, admin yetkisi gerektirir, ne yaptigini bilmeyen ellerde yikici olabilir. Onun yerine ManagementSystemObject kullan. Ornek:

    void Main()
    {
    	var searcher = new ManagementObjectSearcher(@"select * from Win32_DiskDrive");
    	foreach (ManagementObject disk in searcher.Get())
    	{
    		var query = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskDrive.DeviceID='{0}'}}
        WHERE AssocClass = Win32_DiskDriveToDiskPartition",
    		disk.Properties["DeviceID"].Value);
    		foreach (ManagementObject diskPartition in new ManagementObjectSearcher(query).Get())
    		{
    			var query2 = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskPartition.DeviceID='{0}'}}
        WHERE AssocClass = Win32_LogicalDiskToPartition",
    			diskPartition.Properties["DeviceId"].Value);
    
    			foreach (ManagementObject logicalDisk in new ManagementObjectSearcher(query2).Get())
    			{
    				Console.WriteLine("Drive: {0}, DeviceId: {1}, Partition: {2}, DriveLetter: {3}",
    				  disk.Properties["Caption"].Value,
    				  disk.Properties["DeviceId"].Value,
    				  diskPartition.Properties["DeviceId"].Value,
    				  logicalDisk.Properties["DeviceID"].Value);
    			}
    		}
    	}
    }



    How to create a Minimal, Reproducible Example
    The way to Go.
    World's most advanced open source (object-) relational Database.
    Flutter (for mobile, for web & desktop.


    15 Ağustos 2019 Perşembe 09:48
    Yanıtlayıcı
  • // Console.WriteLine( PhysicalInfo("G") );
    // Console.WriteLine( PhysicalInfo("G:") );
    // Console.WriteLine( PhysicalInfo("G:\") );
    
    
    
    private string PhysicalInfo(string drive)
    {
    	var searcher = new ManagementObjectSearcher(@"select * from Win32_DiskDrive");
    	foreach (ManagementObject disk in searcher.Get())
    	{
    		var query = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskDrive.DeviceID='{0}'}}
        WHERE AssocClass = Win32_DiskDriveToDiskPartition",
    		disk.Properties["DeviceID"].Value);
    		foreach (ManagementObject diskPartition in new ManagementObjectSearcher(query).Get())
    		{
    			var query2 = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskPartition.DeviceID='{0}'}}
        WHERE AssocClass = Win32_LogicalDiskToPartition",
    			diskPartition.Properties["DeviceId"].Value);
    			foreach (ManagementObject logicalDisk in new ManagementObjectSearcher(query2).Get())
    			{
    				if ((string)logicalDisk.Properties["DeviceID"].Value == $"{drive[0]}:".ToUpper())
    				{
    					return (string)disk.Properties["DeviceId"].Value;
    				}
    			}
    		}
    	}
    	return null;
    }



    How to create a Minimal, Reproducible Example
    The way to Go.
    World's most advanced open source (object-) relational Database.
    Flutter (for mobile, for web & desktop.

    • Yanıt Olarak İşaretleyen Faruk GÖKÇE 14 Ekim 2019 Pazartesi 11:51
    15 Ağustos 2019 Perşembe 16:44
    Yanıtlayıcı

Tüm Yanıtlar

  • Oldukca kotu bir kodu ornek almissin. Diskpart hem kullanımı tehlikeli, admin yetkisi gerektirir, ne yaptigini bilmeyen ellerde yikici olabilir. Onun yerine ManagementSystemObject kullan. Ornek:

    void Main()
    {
    	var searcher = new ManagementObjectSearcher(@"select * from Win32_DiskDrive");
    	foreach (ManagementObject disk in searcher.Get())
    	{
    		var query = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskDrive.DeviceID='{0}'}}
        WHERE AssocClass = Win32_DiskDriveToDiskPartition",
    		disk.Properties["DeviceID"].Value);
    		foreach (ManagementObject diskPartition in new ManagementObjectSearcher(query).Get())
    		{
    			var query2 = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskPartition.DeviceID='{0}'}}
        WHERE AssocClass = Win32_LogicalDiskToPartition",
    			diskPartition.Properties["DeviceId"].Value);
    
    			foreach (ManagementObject logicalDisk in new ManagementObjectSearcher(query2).Get())
    			{
    				Console.WriteLine("Drive: {0}, DeviceId: {1}, Partition: {2}, DriveLetter: {3}",
    				  disk.Properties["Caption"].Value,
    				  disk.Properties["DeviceId"].Value,
    				  diskPartition.Properties["DeviceId"].Value,
    				  logicalDisk.Properties["DeviceID"].Value);
    			}
    		}
    	}
    }



    How to create a Minimal, Reproducible Example
    The way to Go.
    World's most advanced open source (object-) relational Database.
    Flutter (for mobile, for web & desktop.


    15 Ağustos 2019 Perşembe 09:48
    Yanıtlayıcı
  • @CetinBasoz hocam,

    Çok teşekkür ederim.

    Tamda istediğim buydu.

    Allah razı olsun sizden.

    İyi çalışmalar.


    Faruk G&amp;amp;#214;K&amp;amp;#199;E

    15 Ağustos 2019 Perşembe 10:16
  • Oldukca kotu bir kodu ornek almissin. Diskpart hem kullanımı tehlikeli, admin yetkisi gerektirir, ne yaptigini bilmeyen ellerde yikici olabilir. Onun yerine ManagementSystemObject kullan. Ornek:

    void Main()
    {
    	var searcher = new ManagementObjectSearcher(@"select * from Win32_DiskDrive");
    	foreach (ManagementObject disk in searcher.Get())
    	{
    		var query = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskDrive.DeviceID='{0}'}}
        WHERE AssocClass = Win32_DiskDriveToDiskPartition",
    		disk.Properties["DeviceID"].Value);
    		foreach (ManagementObject diskPartition in new ManagementObjectSearcher(query).Get())
    		{
    			var query2 = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskPartition.DeviceID='{0}'}}
        WHERE AssocClass = Win32_LogicalDiskToPartition",
    			diskPartition.Properties["DeviceId"].Value);
    
    			foreach (ManagementObject logicalDisk in new ManagementObjectSearcher(query2).Get())
    			{
    				Console.WriteLine("Drive: {0}, DeviceId: {1}, Partition: {2}, DriveLetter: {3}",
    				  disk.Properties["Caption"].Value,
    				  disk.Properties["DeviceId"].Value,
    				  diskPartition.Properties["DeviceId"].Value,
    				  logicalDisk.Properties["DeviceID"].Value);
    			}
    		}
    	}
    }



    How to create a Minimal, Reproducible Example
    The way to Go.
    World's most advanced open source (object-) relational Database.
    Flutter (for mobile, for web & desktop.


    Çetin hocam,

    <Console.WriteLine("DeviceId: {0}, DriveLetter: {1}, Drive: {2} ",
                                disk.Properties["DeviceId"].Value,
                                logicalDisk.Properties["DeviceID"].Value,
                                disk.Properties["Caption"].Value);

    Aşağıdaki gibi çıktı alıyorum

    DeviceId: \\.\PHYSICALDRIVE1, DriveLetter: D:, Drive: Microsoft Sanal Diski
    DeviceId: \\.\PHYSICALDRIVE0, DriveLetter: C:, Drive: ST500LM021-1KJ152
    DeviceId: \\.\PHYSICALDRIVE3, DriveLetter: F:, Drive: SanDisk Cruzer Edge USB Device
    DeviceId: \\.\PHYSICALDRIVE3, DriveLetter: G:, Drive: SanDisk Cruzer Edge USB Device
    DeviceId: \\.\PHYSICALDRIVE2, DriveLetter: E:, Drive: Microsoft Sanal Diski

    Bu çıktı içerisinde örnek veriyorum G sürücüsünü arayıp ona ait olan \\.\PHYSICALDRIVE3 bilgisini nasıl alabilirim?

    Tabloya atıp içinde for ile aramayı denedim ancak başaramadım. Bu konuda da yardımcı olabilir misiniz?


    Faruk G&amp;amp;#214;K&amp;amp;#199;E

    15 Ağustos 2019 Perşembe 12:05
  • // Console.WriteLine( PhysicalInfo("G") );
    // Console.WriteLine( PhysicalInfo("G:") );
    // Console.WriteLine( PhysicalInfo("G:\") );
    
    
    
    private string PhysicalInfo(string drive)
    {
    	var searcher = new ManagementObjectSearcher(@"select * from Win32_DiskDrive");
    	foreach (ManagementObject disk in searcher.Get())
    	{
    		var query = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskDrive.DeviceID='{0}'}}
        WHERE AssocClass = Win32_DiskDriveToDiskPartition",
    		disk.Properties["DeviceID"].Value);
    		foreach (ManagementObject diskPartition in new ManagementObjectSearcher(query).Get())
    		{
    			var query2 = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskPartition.DeviceID='{0}'}}
        WHERE AssocClass = Win32_LogicalDiskToPartition",
    			diskPartition.Properties["DeviceId"].Value);
    			foreach (ManagementObject logicalDisk in new ManagementObjectSearcher(query2).Get())
    			{
    				if ((string)logicalDisk.Properties["DeviceID"].Value == $"{drive[0]}:".ToUpper())
    				{
    					return (string)disk.Properties["DeviceId"].Value;
    				}
    			}
    		}
    	}
    	return null;
    }



    How to create a Minimal, Reproducible Example
    The way to Go.
    World's most advanced open source (object-) relational Database.
    Flutter (for mobile, for web & desktop.

    • Yanıt Olarak İşaretleyen Faruk GÖKÇE 14 Ekim 2019 Pazartesi 11:51
    15 Ağustos 2019 Perşembe 16:44
    Yanıtlayıcı
  • // Console.WriteLine( PhysicalInfo("G") );
    // Console.WriteLine( PhysicalInfo("G:") );
    // Console.WriteLine( PhysicalInfo("G:\") );
    
    
    
    private string PhysicalInfo(string drive)
    {
    	var searcher = new ManagementObjectSearcher(@"select * from Win32_DiskDrive");
    	foreach (ManagementObject disk in searcher.Get())
    	{
    		var query = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskDrive.DeviceID='{0}'}}
        WHERE AssocClass = Win32_DiskDriveToDiskPartition",
    		disk.Properties["DeviceID"].Value);
    		foreach (ManagementObject diskPartition in new ManagementObjectSearcher(query).Get())
    		{
    			var query2 = string.Format(@"ASSOCIATORS OF 
        {{Win32_DiskPartition.DeviceID='{0}'}}
        WHERE AssocClass = Win32_LogicalDiskToPartition",
    			diskPartition.Properties["DeviceId"].Value);
    			foreach (ManagementObject logicalDisk in new ManagementObjectSearcher(query2).Get())
    			{
    				if ((string)logicalDisk.Properties["DeviceID"].Value == $"{drive[0]}:".ToUpper())
    				{
    					return (string)disk.Properties["DeviceId"].Value;
    				}
    			}
    		}
    	}
    	return null;
    }



    How to create a Minimal, Reproducible Example
    The way to Go.
    World's most advanced open source (object-) relational Database.
    Flutter (for mobile, for web & desktop.

    @ÇetinBasoz hocam merhaba,

    Yukarıdaki verdiğiniz kodlardan seçilen diskin MBR mi GPT mi olduğu bilgisini nasıl alabilirim? Bir kaç deneme yaptım fakat çözüm bulamadım. Yardımlarınızı rica ediyorum.

    Teşekkürler.


    Faruk G&amp;amp;#214;K&amp;amp;#199;E

    14 Ekim 2019 Pazartesi 11:51
  • Bilmiyorum.


    How to create a Minimal, Reproducible Example
    The way to Go.
    World's most advanced open source (object-) relational Database.
    Flutter (for mobile, for web & desktop.

    14 Ekim 2019 Pazartesi 12:06
    Yanıtlayıcı