禁用LAN连接

时间:2011-05-17 04:51:03

标签: c#-4.0

我的系统连接了LAN连接。

我需要禁用我的LAN连接,

我怎么能用C#程序

来做这件事

你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

正如我在SuperUser上回答的那样 - 只是在这里添加它,因为它应该只在SO上:

您可以从命令行禁用/启用NIC:

netsh interface set interface “Local Area Connection” disabled
netsh interface set interface “Local Area Connection” enabled

将“Local Area Connection”替换为您要禁用的网络接口的名称。

您可以使用以下内容从C#调用此方法:

启用

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

答案 1 :(得分:0)

Public Shared Sub ToggleWirelessConnection()
    For Each verb As Shell32.FolderItemVerb In WirelessConnectionFolderItem.Verbs
        If verb.Name = "En&able" OrElse verb.Name = "Disa&ble" Then
            verb.DoIt()
            Exit For
        End If
    Next
    Threading.Thread.Sleep(1000)
End Sub

这是VB中的代码,它可以工作。

相关问题