How to disable/enable network connection in c#

后端 未结 7 1607
孤街浪徒
孤街浪徒 2020-11-30 05:55

Basically I\'m running some performance tests and don\'t want the external network to be the drag factor. I\'m looking into ways of disabling network LAN. What is an effecti

7条回答
  •  天命终不由人
    2020-11-30 06:14

    For Windows 10 Change this: for diable ("netsh", "interface set interface name=" + interfaceName + " admin=DISABLE") and for enable ("netsh", "interface set interface name=" + interfaceName + " admin=ENABLE") And use the program as Administrator

        static void Disable(string interfaceName)
        {
    
            //set interface name="Ethernet" admin=DISABLE
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface name=" + interfaceName + " admin=DISABLE");
            System.Diagnostics.Process p = new System.Diagnostics.Process();
    
            p.StartInfo = psi;
            p.Start();
        }
    
        static void Enable(string interfaceName)
        {
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface name=" + interfaceName + " admin=ENABLE");
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = psi;
            p.Start();
        }
    

    And use the Program as Administrator !!!!!!

提交回复
热议问题