How to disable/enable network connection in c#

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

    best solution is disabling all network adapters regardless of the interface name is disabling and enabling all network adapters using this snippet (Admin rights needed for running , otherwise ITS WONT WORK) :

      static void runCmdCommad(string cmd)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = $"/C {cmd}";
            process.StartInfo = startInfo;
            process.Start();
        }
       static void DisableInternet(bool enable)
        {
            string disableNet = "wmic path win32_networkadapter where PhysicalAdapter=True call disable";
            string enableNet = "wmic path win32_networkadapter where PhysicalAdapter=True call enable";
            runCmdCommad(enable ? enableNet :disableNet);
        }
    

提交回复
热议问题