在昨天的随笔里:两个硬盘和文件相关的小技巧 中对于取得硬盘的分区列表使用了API,在灵感之源的提醒下,联想到用WMI来实现,现在已经成功了,感谢灵感之源提供的思路
以下是示例代码:
先要引用System.Management.dll,
using System.Management;
如果查询语句中的DriveType 为2,会得到软驱,如果为5,会得到光驱。
以下是示例代码:
先要引用System.Management.dll,
using System.Management;
SelectQuery query =
new SelectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
foreach (ManagementBaseObject envVar in searcher.Get())
{
this.listBox1.Items.Add(envVar["Name"].ToString());
}
new SelectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
foreach (ManagementBaseObject envVar in searcher.Get())
{
this.listBox1.Items.Add(envVar["Name"].ToString());
}
如果查询语句中的DriveType 为2,会得到软驱,如果为5,会得到光驱。