4

When I open Disk Management (right click My Computer->Manage) I see: enter image description here

How can I know that path F:\ belongs to Disk5? In other words I will like to know what disks are available with C#.

The reason why I need to know that is because I have a usb mas storage device that is encrypted and I need to pass the parameter \Device\Harddisk5 to TrueCrypt along with the password in order to mount the encrypted device with code.

Edit

I know how to look the drives info. I just dont konw how to know that Drive 1 belongs to disk 0 for instance. In other words I am having trouble figuring out the Disk Number. I am looking to implement:

public string GetDiskNumber(char letter) { // implenetation return Disk5; } 

where I will call that as:

GetDiskNumber('F'); 
2
  • I assume you are doing this via C# Code..? have you looked at using WMI Commented Jan 23, 2014 at 23:10
  • WMI is what I would use to get this information as well, check out these classes Win32_DiskDrive and Win32_DiskPartition Commented Jan 23, 2014 at 23:23

2 Answers 2

5

You can use WMI to retrieve that information

System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=" & DriveLetter & ":") 

See more at Win32_LogicalDisk class I hope it helps. By the way there is PInvoke too GetVolumeInformation.

If you need 'PHYSICALDRIVE0' you should use Win32_PhysicalMedia class and the class Win32_DiskDrivePhysicalMedia glue both.

An exemple of your need in C#

public string GetDiskNumber(string letter) { var ret = "0"; var scope = new ManagementScope("\\\\.\\ROOT\\cimv2"); var query = new ObjectQuery("Associators of {Win32_LogicalDisk.DeviceID='" + letter + ":'} WHERE ResultRole=Antecedent"); var searcher = new ManagementObjectSearcher(scope, query); var queryCollection = searcher.Get(); foreach (ManagementObject m in queryCollection) { ret = m["Name"].ToString().Replace("Disk #", "")[0].ToString(); } return ret; } 
Sign up to request clarification or add additional context in comments.

2 Comments

That gives a lot of info. when I try System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=c:") for instance how can I know its drive 0?
If you need the name like 'PHYSICALDRIVE0' you should Win32_PhysicalMedia class.
2

Have made a method for you gets drive letter and its number in a dictionary;

public Dictionary<string, string> GetDrives() { var result = new Dictionary<string, string>(); foreach ( var drive in new ManagementObjectSearcher( "Select * from Win32_LogicalDiskToPartition" ).Get().Cast<ManagementObject>().ToList() ) { var driveLetter = Regex.Match( (string)drive[ "Dependent" ], @"DeviceID=""(.*)""" ).Groups[ 1 ].Value; var driveNumber = Regex.Match( (string)drive[ "Antecedent" ], @"Disk #(\d*)," ).Groups[ 1 ].Value; result.Add( driveLetter, driveNumber ); } return result; } 

2 Comments

That does not tell me what drive number drive F is :(
Works too I tried other answer first. +1 for great help! thanks so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.