62208

See disk management info with c#

When I open Disk Management (right click My Computer->Manage) I see: <img src="https://i.stack.imgur.com/CnPdZ.png" alt="enter image description here">

<strong>How can I know that path F:\ belongs to Disk5?</strong> 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 <strong>Disk Number</strong>. I am looking to implement:

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

where I will call that as:

GetDiskNumber('F');

Answer1:

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; }

Answer2:

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; }

Recommend

  • what is the memory management in cocos2dx 3.10?
  • Hadoop: How to include third party library in Python MapReduce [duplicate]
  • expression language: quick way to get maximum of a collection
  • Visual Studio 2015 Using Incorrect Cordova Version
  • Search feature with multiple criteria - PHP/MySQL
  • How do you get the value from a TVML textField?
  • Julia custom type assignment
  • Trying to use only a portion of a SKTexture using textureWithRect inTexture
  • Using QFileSystemModel to display only one directory
  • LINQ To XML Syntax for XML Element with Attributes
  • Django - Subdomains with Middleware or Subdomains with Apache Virtual Hosts?
  • readyRead() signal of QextSerialPort (QIODevice) is not being called fast enough
  • Neo4J / py2neo — cursor-based query?
  • How to read a certificate from a USB token in C#
  • Client-Side: Accessing Windows Azure Drive?
  • Read files from URL
  • Why is RAM in powers of 2?
  • Serializing socket
  • Collect HostCPU utilisation and disk I/O speed using XenServer Java API
  • Are there algorithms for putting a digest into the file being digested?
  • How do you compute the XOR Remainder used in CRC?
  • How to get ID of changed file on Google Drive
  • Accessing the variables from a PHP Anonymous Function
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Simple linked list-C
  • Redshift Querying: error xx000 disk full redshift
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Is there a javascript serializer for JSON.Net?
  • Where to put my custom functions in Wordpress?
  • Large data - storage and query
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • Buffer size for converting unsigned long to string
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal