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