基本上,我正在运行一些性能测试,不希望外部网络成为制约因素。我正在研究以编程方式禁用网络LAN的方法。如果有任何人有一个能够让人明白的c#代码片段,那就太棒了。
基本上,我正在运行一些性能测试,不希望外部网络成为制约因素。我正在研究以编程方式禁用网络LAN的方法。如果有任何人有一个能够让人明白的c#代码片段,那就太棒了。
在搜索相同问题时发现了这个帖子,所以,这里是答案 :)
C#中我测试过的最佳方法使用WMI。
http://www.codeproject.com/KB/cs/EverythingInWmi02.aspx
C#片段:(在解决方案中必须引用System.Management,并在using声明中使用)
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);
}
}
使用netsh命令,您可以启用和禁用“本地连接”
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();
}
System.Diagnostics.Process.Start("ipconfig", "/release"); //For disabling internet
System.Diagnostics.Process.Start("ipconfig", "/renew"); //For enabling internet
请确保以管理员身份运行。希望对您有所帮助!
如果要在 Windows 10 上禁用网络接口,请使用以下命令: ("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();
}
并且需要以管理员身份运行程序!!!!!!
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
最佳解决方案是禁用所有网络适配器,无论接口名称是否被禁用,并使用此代码片段禁用和启用所有网络适配器(需要管理员权限才能运行,否则将无法工作):
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);
}
我修改了最佳投票解决方案,来自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();
}
看到其他答案,虽然有些有效,但有些不行。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;
}
}