2013-08-18 29 views
0

我得到一個帶有GetBestInterface(iphlpapi.dll)的InterfaceIndex。c#NetworkInterface.GetAllNetworkInterfaces vs WMI Win32_NetworkAdapter

目標:讀取其他接口屬性。

此WMI查詢速度慢:

SELECT MACAddress,Name,GUID,NetConnectionID FROM Win32_NetworkAdapter WHERE InterfaceIndex= 

在C#中有,速度更快,

NetworkInterface.GetAllNetworkInterfaces() 

但每個NetworkInterface的卻沒有財產InterfaceIndex(原文如此!)。

我不知道如何優化這樣的:似乎

EnumerationOptions wmi_options = new EnumerationOptions(); 
    wmi_options.Rewindable = false; 
    wmi_options.ReturnImmediately = true; 
    string wql = "SELECT MACAddress,Name,GUID,NetConnectionID FROM Win32_NetworkAdapter WHERE InterfaceIndex=" + iface; 
    ManagementObjectCollection recordset = new ManagementObjectSearcher(@"root\cimv2", wql, wmi_options).Get(); 
    foreach (ManagementObject mo in recordset) 

的選項不能幫助。 我可以拆分操作並緩存任何步驟嗎?

或其他路徑:避免WMI並從註冊表中查找接口(與InterfaceIndex)?

HKLM \系統\ CurrentControlSet \服務\ TCPIP \參數\接口

HKLM \系統\ CurrentControlSet \控制\網絡{4D36E972-E325-11CE-BFC1-08002BE10318}

+0

我發現並給予好評的[解決方案] [1] [1]:http://stackoverflow.com/a/11059702/471232 – Massimo

+0

所以我的最後一個問題是,在一般:如何緩存和速度 - up wmi查詢? – Massimo

回答

0

我不知道這是否更快,但你有沒有嘗試過PowerShell?

string IfIndex = "15"; 
     string psscript = "get-netadapter | Where-Object interfaceindex -eq " + IfIndex; 
     PowerShell powershell = PowerShell.Create(); 
     powershell.Runspace = RunspaceFactory.CreateRunspace(); 
     powershell.Runspace.Open(); 
     powershell.AddScript(psscript); 
     foreach (PSObject result in powershell.Invoke()) 
     { 
      Console.WriteLine("Name: {0} " + "Status: {1}",result.Members["Name"].Value, result.Members["status"].Value); 
     } 
     Console.Read(); 

從您的應用程序運行它,使用System.Management.Automation & System.Management.Automation.Runspaces;

我還沒有測試代碼,這是我的頭頂。