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 effective way of doing it programmatically? I'm interested in c#. If anyone has a code snippet that can drive the point home that would be cool.
-
[Something similar](http://stackoverflow.com/questions/83756/how-to-programmatically-enabledisable-network-interfaces-windows-xp) has been discussed. – hayalci Oct 06 '08 at 00:08
-
Yeah, but this has actual working C# code. – Colin Jan 10 '13 at 00:37
-
Please follow below link. it will may help you. [http://stackoverflow.com/questions/3053372/programmatically-enable-disable-connection](http://stackoverflow.com/questions/3053372/programmatically-enable-disable-connection) – Buddhika Samith Nov 10 '16 at 06:28
-
Good question seems to have long-term relevance. Upvoting the question for value. Lots of good answers, probably better not to identify a single one as The answer. Sometimes easier is better and if you just want to toggle the network, netsh in a .bat file on the desktop might do the trick. But I came here looking for code examples too for detecting and toggling the connection, so the code here is great - even 6 years later. – TonyG Jan 09 '18 at 18:46
8 Answers
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
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);
}
}

- 622
- 10
- 11
-
8Note that I had to run this as Administrator (the InvokeMethod() did not return an error when I ran it as a peon). – Colin Jan 10 '13 at 00:36
-
-
Just used the code in a .NET project on Windows Build 20H2. Run as administrator and it takes down the adpater. I also used the member NetConnectionId to check for the occurrance of "WLAN" and "Ethernet" to not to take down the Bluetooth-Adapter. A good explanation of Win32_NetworkAdapter can be found here: https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapter – Nonlinearsound Feb 03 '22 at 20:03
Using netsh Command, you can enable and disable “Local Area Connection”
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();
}

- 13,678
- 8
- 61
- 92

- 231
- 2
- 4
-
1After some hours suffering with WMI (apparently not every adapter offers the `Disable` command, or something like that), `netsh` worked smoothly on the first try. – heltonbiker Jan 15 '15 at 18:05
-
1
-
It Worked nice for me! I though it would be hard but you make it ez for me. Tnx. – Mostafa Fahimi Feb 26 '16 at 20:05
-
Worked fine. Prefer this method method to /release as I don't want to be changing IP address. X – TinyRacoon Mar 09 '21 at 09:14
If you are looking for a very simple way to do it, here you go:
System.Diagnostics.Process.Start("ipconfig", "/release"); //For disabling internet
System.Diagnostics.Process.Start("ipconfig", "/renew"); //For enabling internet
Make sure you run as administrator. I hope you found this helpful!
For Windows 10 Change this: for diable ("netsh", "interface set interface name=" + interfaceName + " admin=DISABLE") and for enable ("netsh", "interface set interface name=" + interfaceName + " admin=ENABLE") And use the program as Administrator
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();
}
And use the Program as Administrator !!!!!!

- 21
- 1
In VB.Net , You can also use it for toggle Local Area Connection
Note: myself use it in Windows XP, it's work here properly. but in windows 7 it's not work properly.
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

- 231
- 2
- 4
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);
}

- 395
- 5
- 8
I modfied the best voted solution from Kamrul Hasan to one methode and added to wait for the exit of the process, cause my Unit Test code run faster than the process disable the connection.
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();
}

- 957
- 8
- 25
Looking at the other answers here, whilst some work, some do not. Windows 10 uses a different netsh command to the one that was used earlier in this chain. The problem with other solutions, is that they will open a window that will be visible to the user (though only for a fraction of a second). The code below will silently enable/disable a network connection.
The code below can definitely be cleaned up, but this is a nice start.
*** Please note that it must be run as an administrator to work ***
//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;
}
}

- 1,773
- 1
- 14
- 18