我想檢索系統中固定磁盤的列表。但是C#的GetDrives固定驅動器包括插入USB硬盤。C#getdrives類型固定但沒有usb harddisks?
任何想法如何檢測到固定硬盤不是USB硬盤或vica?
我想檢索系統中固定磁盤的列表。但是C#的GetDrives固定驅動器包括插入USB硬盤。C#getdrives類型固定但沒有usb harddisks?
任何想法如何檢測到固定硬盤不是USB硬盤或vica?
使用的DriveType來檢測驅動器的類型:
using System.IO;
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady && d.DriveType == DriveType.Fixed)
{
// This is the drive you want...
}
}
EDIT1:
檢查下面的鏈接: How do I detected whether a hard drive is connected via USB?
解從How to get serial number of USB-Stick in C#缺口:
//import the System.Management namespace at the top in your "using" statement.
ManagementObjectSearch theSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
使用此MSDN鏈接,個性化解決方案(包括尋找盤符): WMI Tasks: Disks and File Systems
在這裏你可以得到USB硬盤的列表。
//Add Reference System.Management and use namespace at the top of the code.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject queryObj in searcher.Get())
{
foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition"))
{
foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
{
Console.WriteLine(String.Format("{0}" + "\\", c["Name"].ToString())); // here it will print USB drive letter
}
}
}
在這裏你可以得到所有固定驅動器的列表(系統和USB硬盤):
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady == true && d.DriveType == DriveType.Fixed)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" Drive type: {0}", d.DriveType);
}
}
如果你對它們進行比較,那麼您可以在一個系統,但沒有獲取固定磁盤列表USB硬盤。
對不起,但互聯網被關閉,檢查下面的鏈接... – 2009-11-03 09:47:46