如何在C#中禁用/启用网络连接 [英] How to disable/enable network connection in c#

查看:572
本文介绍了如何在C#中禁用/启用网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在运行一些性能测试,并且不希望外部网络成为拖累因素.我正在研究禁用网络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

msdn上的Win32_NetworkAdapter

C#代码段:(必须在解决方案中以及在使用声明中引用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#中禁用/启用网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆