2012-05-14 40 views
1

夥計們,查詢win32_NetworkConnection是

多年來慢在Windows 7上,我一直愉快地使用下面的C#IsNetworkDrive方法來確定「這是一個網絡驅動器?」在Windows XP下。它仍然在Windows 7下產生預期的結果,但每次調用需要10秒的時間...... ergo大概有一百萬次太長!

using System; 
using System.Collections.Generic; 
using System.Management; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      foreach (var driveName in Environment.GetLogicalDrives()) 
       Console.WriteLine(driveName + " " + IsNetworkDrive(driveName.Substring(0,2))); 
      Console.Write("Press any key to continue ..."); 
      Console.ReadKey(); 
     } 

     private static bool IsNetworkDrive(string drive) 
     { 
      var query = @"SELECT Name FROM win32_NetworkConnection WHERE LocalName='" + drive + "'"; 
      var seacher = new ManagementObjectSearcher(query); 
      var resultset = seacher.Get(); 
      var count = resultset.Count; 
      return count == 1; 
     } 

    } 
} 

Arrrgggghhhh。我討厭Windows!

請問任何人有任何想法如何[執行]確定一個給定的驅動器號代表本地驅動器或網絡驅動器,在Windows 7和Windows XP(企業SOE的)?

[.NET中最好使用.NET < = 3.5,儘管我可以將項目升級到4.0,但它只需要一個綠地測試,我們現在沒有錢。感嘆。]

乾杯。基思。

回答

1

我在回答我自己的問題。

更google搜索導致我這個帖子位:How to programmatically discover mapped network drives on system and their server names?

而在這個問題的代碼使我:DriveInfo.GetDrives()我爲我的主要工作方法:

static void Main(string[] args) { 
     foreach (var driveInfo in DriveInfo.GetDrives()) 
      Console.WriteLine(driveInfo + " " + driveInfo.DriveType); 
     Console.Write("Press any key to continue ..."); 
     Console.ReadKey(); 
    } 

生產:

C:\ Fixed 
D:\ CDRom 
E:\ Removable 
G:\ Network 
H:\ Network 
I:\ Network 
K:\ Network 
L:\ Network 
M:\ Network 
Q:\ Network 
Press any key to continue ... 

...然後它的不遠處還有到:

// Example drives: "C:" or "L:" ... just the drive-letter and a colon. No backslash. 
    private static bool IsNetworkDrive(string drive) { 
     return new DriveInfo(drive).DriveType == DriveType.Network; 
    } 

它在Windows 7下以期望的[亞秒]時間幀產生預期結果,以非管理員身份運行。

希望這可以幫助別人。

乾杯。基思。