How to disable/enable network connection in c#

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

    Found this thread while searching for the same thing, so, here is the answer :)

    The best method I tested in C# uses WMI.

    http://www.codeproject.com/KB/cs/EverythingInWmi02.aspx

    Win32_NetworkAdapter on msdn

    C# Snippet : (System.Management must be referenced in the solution, and in using declarations)

    SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
    ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
    foreach (ManagementObject item in searchProcedure.Get())
    {
        if (((string)item["NetConnectionId"]) == "Local Network Connection")
        {
           item.InvokeMethod("Disable", null);
        }
    }
    

提交回复
热议问题