How to disable/enable network connection in c#

后端 未结 7 1626
孤街浪徒
孤街浪徒 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:22

    I modfied the best voted solution from Kamrul Hasan to one methode and added to wait for the exit of the process, cause my Unit Test code run faster than the process disable the connection.

    private void Enable_LocalAreaConection(bool isEnable = true)
        {
            var interfaceName = "Local Area Connection";
            string control;
            if (isEnable)
                control = "enable";
            else
                control = "disable";
            System.Diagnostics.ProcessStartInfo psi =
                   new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" " + control);
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = psi;
            p.Start();
            p.WaitForExit();
        }
    

提交回复
热议问题