问题描述
基本上我正在运行一些性能测试并且不希望外部网络成为阻力因素.我正在研究禁用网络 LAN 的方法.以编程方式执行此操作的有效方法是什么?我对 c# 感兴趣.如果有人有一个代码片段可以把重点带回家,那就太好了.
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.
推荐答案
在搜索相同内容时发现此线程,所以,这里是答案:)
Found this thread while searching for the same thing, so, here is the answer :)
我在 C# 中测试的最佳方法使用 WMI.
The best method I tested in C# uses WMI.
http://www.codeproject.com/KB/cs/EverythingInWmi02.aspx
C# 代码段:(必须在解决方案和 using 声明中引用 System.Management)
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);
}
}
这篇关于如何在 C# 中禁用/启用网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!