2015-05-25 22 views
0

我正在創建一個小工具來管理我的NetworkInterfaces。 我已經在網上查找了一種禁用nic的方法。我已經有了它的一個實例。試圖禁用NetworkInterface

private void disableNic() 
    { 
     SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL"); 
     ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery); 
     foreach (ManagementObject item in searchProcedure.Get()) 
     { 
      if (((string)item["NetConnectionId"]) == Interface.Name) 
      { 
       item.InvokeMethod("Disable", null); 
      } 
     } 
    } 

    void Disable(string interfaceName) 
    { 
     System.Diagnostics.ProcessStartInfo psi = 
      new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" disable"); 
     System.Diagnostics.Process p = new System.Diagnostics.Process(); 
     p.StartInfo = psi; 
     p.Start(); 
    } 

這些函數都沒有產生我想要的結果(事實上它們什麼都不做)。我只想禁用NetworkInterface。

有誰知道一個更好的方式來做到這一點? 非常感謝!

+0

如果您不檢查錯誤,那麼您永遠無法找出爲什麼「它不工作」。不要忽略InvokeMethod()的返回值。 –

回答

0

嘗試使用管理員權限運行應用程序

+0

好的,哇。 從來沒有想過這是工作。謝謝! – SnelleJelle