29

基本上我正在运行一些性能测试并且不希望外部网络成为拖累因素。我正在研究禁用网络 LAN 的方法。以编程方式执行此操作的有效方法是什么?我对c#很感兴趣。如果有人有一个可以将要点带回家的代码片段,那将是很酷的。

4

8 回答 8

35

在搜索相同的东西时发现了这个线程,所以,这是答案:)

我在 C# 中测试的最佳方法是使用 WMI。

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

msdn 上的 Win32_NetworkAdapter

C# 片段:(必须在解决方案和使用声明中引用 System.Management)

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);
    }
}
于 2011-05-29T21:22:29.040 回答
17

使用 netsh 命令,您可以启用和禁用“本地连接”</p>

  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();
    }
于 2013-09-12T10:01:32.040 回答
4

如果您正在寻找一种非常简单的方法来做到这一点,那么您可以:

    System.Diagnostics.Process.Start("ipconfig", "/release"); //For disabling internet
    System.Diagnostics.Process.Start("ipconfig", "/renew"); //For enabling internet

确保以管理员身份运行。我希望你觉得这很有帮助!

于 2019-06-15T14:13:52.047 回答
1

在 VB.Net 中,您还可以使用它来切换本地连接

注意:我自己在 Windows XP 中使用它,它在这里正常工作。但在 Windows 7 中它不能正常工作。

  Private Sub ToggleNetworkConnection()

    Try


        Const ssfCONTROLS = 3


        Dim sConnectionName = "Local Area Connection"

        Dim sEnableVerb = "En&able"
        Dim sDisableVerb = "Disa&ble"

        Dim shellApp = CreateObject("shell.application")
        Dim WshShell = CreateObject("Wscript.Shell")
        Dim oControlPanel = shellApp.Namespace(ssfCONTROLS)

        Dim oNetConnections = Nothing
        For Each folderitem In oControlPanel.items
            If folderitem.name = "Network Connections" Then
                oNetConnections = folderitem.getfolder : Exit For
            End If
        Next


        If oNetConnections Is Nothing Then
            MsgBox("Couldn't find 'Network and Dial-up Connections' folder")
            WshShell.quit()
        End If


        Dim oLanConnection = Nothing
        For Each folderitem In oNetConnections.items
            If LCase(folderitem.name) = LCase(sConnectionName) Then
                oLanConnection = folderitem : Exit For
            End If
        Next


        If oLanConnection Is Nothing Then
            MsgBox("Couldn't find '" & sConnectionName & "' item")
            WshShell.quit()
        End If


        Dim bEnabled = True
        Dim oEnableVerb = Nothing
        Dim oDisableVerb = Nothing
        Dim s = "Verbs: " & vbCrLf
        For Each verb In oLanConnection.verbs
            s = s & vbCrLf & verb.name
            If verb.name = sEnableVerb Then
                oEnableVerb = verb
                bEnabled = False
            End If
            If verb.name = sDisableVerb Then
                oDisableVerb = verb
            End If
        Next



        If bEnabled Then
            oDisableVerb.DoIt()
        Else
            oEnableVerb.DoIt()
        End If


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
于 2013-09-12T10:54:44.010 回答
1

对于 Windows 10 更改:对于 diable ("netsh", "interface set interface name=" + interfaceName + " admin=DISABLE") 和对于启用 ("netsh", "interface set interface name=" + interfaceName +" admin= ENABLE") 并以管理员身份使用该程序

    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();
    }

并以管理员身份使用程序!!!!!!

于 2018-02-07T19:08:28.783 回答
1

最好的解决方案是禁用所有网络适配器,无论接口名称如何,都使用此代码段禁用和启用所有网络适配器(运行需要管理员权限,否则它不会工作):

  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);
    }
于 2019-08-26T07:38:58.937 回答
0

我将来自Kamrul Hasan的最佳投票解决方案修改为一种方法并添加以等待进程退出,导致我的单元测试代码运行得比进程禁用连接更快。

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();
    }
于 2017-09-08T09:42:11.320 回答
0

在这里查看其他答案,虽然有些工作,但有些没有。Windows 10 使用与此链中较早使用的不同的 netsh 命令。其他解决方案的问题在于,它们将打开一个用户可见的窗口(尽管只有几分之一秒)。下面的代码将静默启用/禁用网络连接。

下面的代码绝对可以清理,但这是一个不错的开始。

***请注意,它必须以管理员身份运行才能工作***

//Disable network interface
static public void Disable(string interfaceName)
{
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.FileName = "netsh";
    startInfo.Arguments = $"interface set interface \"{interfaceName}\" disable";
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    System.Diagnostics.Process processTemp = new System.Diagnostics.Process();
    processTemp.StartInfo = startInfo;
    processTemp.EnableRaisingEvents = true;
    try
    {
        processTemp.Start();
    }
    catch (Exception e)
    {
        throw;
    }
}

//Enable network interface
static public void Enable(string interfaceName)
{
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.FileName = "netsh";
    startInfo.Arguments = $"interface set interface \"{interfaceName}\" enable";
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    System.Diagnostics.Process processTemp = new System.Diagnostics.Process();
    processTemp.StartInfo = startInfo;
    processTemp.EnableRaisingEvents = true;
    try
    {
        processTemp.Start();
    }
    catch (Exception e)
    {
        throw;
    }
}
于 2021-07-25T16:44:27.157 回答