sam
sam

Reputation: 19

Disable LAN connection

My system is connected with a LAN connection.

I need to disable my LAN connection,

How could i do this using C# program

Can you help me please??

Upvotes: 0

Views: 4332

Answers (2)

sam
sam

Reputation: 19

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

This is the code in VB and it works.

Upvotes: 0

Jaymz
Jaymz

Reputation: 6348

As I've answered this over on SuperUser - just adding this here as it should only have been on SO:

You can disable/enable your NIC from the command line:

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

Replace "Local Area Connection" with the name of the Network Interface you want to disable.

You can call this from C# using something like the following:

Enable

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

Disable

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

Upvotes: 1

Related Questions