2009-11-03 43 views
5

我想檢索系統中固定磁盤的列表。但是C#的GetDrives固定驅動器包括插入USB硬盤。C#getdrives類型固定但沒有usb harddisks?

任何想法如何檢測到固定硬盤不是USB硬盤或vica?

+0

對不起,但互聯網被關閉,檢查下面的鏈接... – 2009-11-03 09:47:46

回答

3

使用的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... 
    } 
} 

DriveInfo Class

EDIT1:

檢查下面的鏈接: How do I detected whether a hard drive is connected via USB?

+0

但DriveType.Removable只是USB棒而不是USB硬盤。 From Docu:驅動器是可移動存儲設備,如軟盤驅動器或USB閃存驅動器。 – Ephraim 2009-11-03 08:54:49

+0

USB Harddisk的類型固定正是這個問題! – Ephraim 2009-11-03 08:56:42

+0

這是可能的。你可以在底部查看我的解決方案。 – Parsa 2015-09-08 08:30:13

4

解從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'"); 
+1

正確,雖然這會列出棒和驅動器,並通過屬性快速瀏覽並未發現找到驅動器盤符的簡單方法。 – 2009-11-03 09:26:34

+0

顯然ephraim已經有這些。此解決方案旨在展示如何過濾USB驅動器。這就是爲什麼查詢被寫爲'InterfaceType ='USB'',爲什麼沒有關係,包括棒。 – MSalters 2009-11-03 09:47:49

+0

由於查詢不返回驅動器號,因此無法匹配ManagementObjects和DriveInfo對象。 – Wouter 2013-08-06 11:33:57

1

在這裏你可以得到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硬盤。