How to disable/enable network connection in c#

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

    Using netsh Command, you can enable and disable “Local Area Connection”

      interfaceName is “Local Area Connection”.
    
      static void Enable(string interfaceName)
        {
         System.Diagnostics.ProcessStartInfo psi =
                new System.Diagnostics.ProcessStartInfo("netsh", "interface set interface \"" + interfaceName + "\" enable");
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = psi;
            p.Start();
        }
    
        static 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();
        }
    

提交回复
热议问题