2013-01-31 82 views
4

我想填充一個邏輯驅動器列表的組合框,但我想排除任何映射的驅動器。下面的代碼給了我一個沒有任何過濾的所有邏輯驅動器的列表。如何列出沒有映射驅動器的邏輯驅動器

comboBox.Items.AddRange(Environment.GetLogicalDrives()); 

有沒有可以幫助您確定物理驅動器和映射驅動器之間的方法?

回答

0

,想到一個第一件事是,映射的驅動器將有串\\

另一種方法是更廣泛的,但更可靠的在這裏詳細描述開始:How to programmatically discover mapped network drives on system and their server names?


或者試試呼籲DriveInfo.GetDrives()這將給你的對象更多的元數據,這將有助於你過濾。這裏有一個例子:

http://www.daniweb.com/software-development/csharp/threads/159290/getting-mapped-drives-list

+1

您可以檢查 「\\」 使用'新的URI(drivePath).IsUnc' –

2

可以使用DriveType財產在DriveInfo

DriveInfo[] dis = DriveInfo.GetDrives(); 
foreach (DriveInfo di in dis) 
{ 
    if (di.DriveType == DriveType.Network) 
    { 
     //network drive 
    } 
    } 
+0

爲什麼你需要'System.Management.dll'? –

+0

我的不好。固定... –

4

您可以使用DriveInfo

DriveInfo[] allDrives = DriveInfo.GetDrives(); 
    foreach (DriveInfo d in allDrives) 
    { 
     Console.WriteLine("Drive {0}", d.Name); 
     Console.WriteLine(" File type: {0}", d.DriveType); 
     if(d.DriveType != DriveType.Network) 
     { 
      comboBox.Items.Add(d.Name); 
     } 
    } 

排除驅動器時,該屬性DriveTypeNetwork

0

嘗試使用System.IO.DriveInfo.GetDrives

comboBox.Items.AddRange(
     System.IO.DriveInfo.GetDrives() 
          .Where(di=>di.DriveType != DriveType.Network) 
          .Select(di=>di.Name)); 
0

這是什麼工作對我來說:

DriveInfo[] allDrives = DriveInfo.GetDrives(); 
foreach (DriveInfo d in allDrives) 
{ 
    if (d.IsReady && (d.DriveType == DriveType.Fixed || d.DriveType == DriveType.Removable)) 
    { 
     cboSrcDrive.Items.Add(d.Name); 
     cboTgtDrive.Items.Add(d.Name); 
    } 

}