Programmatically disable/enable network interface

前端 未结 7 994
臣服心动
臣服心动 2020-12-08 11:36

I\'m trying to come up with a solution to programmatically enable/disable the network card - I\'ve done a ton of research and nothing seems to be a workable solution in both

7条回答
  •  渐次进展
    2020-12-08 11:40

    ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapter");
    ManagementObjectCollection mgmtObjectColl = managementClass.GetInstances();
    
    ManagementObject myObject = null;
    
    foreach (ManagementObject mgmtObject in mgmtObjectColl)
    {
        if (mgmtObject["NetConnectionID"] != null && mgmtObject["NetConnectionID"].Equals("Local Area Connection"))
        {
            Console.WriteLine("found");
            myObject = mgmtObject;
            object result = mgmtObject.InvokeMethod("Disable", null);
        }
        //Console.WriteLine("{0}, {1}", mgmtObject["Name"], mgmtObject["NetConnectionID"]);
    }
    
    object result3 = myObject.InvokeMethod("Enable", null);
    

提交回复
热议问题