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.
问题:
回答1:
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);
}
}
回答2:
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();
}
回答3:
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
回答4:
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();
}
回答5:
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 !!!!!!