如何获取可用的盘符?
下面程序是想打印出每个盘符下第一级目录中的所有文件和子目录的名称
static void Main(string[] args)
{
string[] s1,s2;
s1= Directory.GetLogicalDrives();
foreach (string t1 in s1)
{
Console.WriteLine( "盘符={0} ",t1);
// s2=Directory.GetFileSystemEntries(t1);
//foreach(string t2 in s2) Console.WriteLine(t2);
}
}
程序输出:
盘符=A:\
盘符=C:\
盘符=D:\
盘符=E:\
盘符=F:\
盘符=G:\
盘符=H:\
盘符=I:\
如果取消注释:
// s2=Directory.GetFileSystemEntries(t1);
//foreach(string t2 in s2) Console.WriteLine(t2);
程序则抛出异常,因为A:是软驱,G:、H:、I:是虚拟光驱,对它们调用GetFileSystemEntries()函数报错。现在我想在执行s2=Directory.GetFileSystemEntries(t1);语句之前,判断哪些是当前可用盘符,应该怎么做?
------解决方案--------------------ManagementObjectSearcher disksearcher =new ManagementObjectSearcher( "select * from win32_logicaldisk ")
Foreach(ManagementObject Disk in disksearcher.Get())
{
Console.WriteLine( "Disk = " & Disk( "Statusinfo "));
}
至于win32_logicaldisk的其它信息可以在http://msdn2.microsoft.com/en-us/library/aa394173.aspx中找到