使用C#查看磁盘管理信息 [英] See disk management info with c#

查看:206
本文介绍了使用C#查看磁盘管理信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打开磁盘管理(右键单击我的电脑->管理)时,我看到:

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

我怎么知道该路径 F:\ \ 属于Disk5吗?换句话说,我想知道C#可以使用哪些磁盘。

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

我需要知道的原因是因为我有一个加密的usb mas存储设备,需要传递参数 \ Device\Harddisk5 TrueCrypt 以及密码,以便安装加密的

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.

我知道如何查看驱动器信息。我只是不知道如何知道驱动器1例如属于磁盘0。换句话说,我在计算磁盘号时遇到了麻烦。我希望实现:

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

在这里我将其称为:

GetDiskNumber('F');


推荐答案

您可以使用WMI检索该信息

You can use WMI to retrieve that information

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

更多信息,请参见 Win32_LogicalDisk类希望对您有所帮助。顺便说一句,也有PInvoke GetVolumeInformation

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

如果您需要 PHYSICALDRIVE0,则应使用 Win32_PhysicalMedia类和类 Win32_DiskDrivePhysicalMedia 粘合。

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

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

这篇关于使用C#查看磁盘管理信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆