[英]How to detect the state of a network interface whether it is enabled?
如何检测网络接口是否启用/禁用?
public Boolean IsNicEnabled(String nic) { return true; }
You could take a look at System.Net.NetworkInformation.NetworkInterface class about this. 您可以查看有关此内容的System.Net.NetworkInformation.NetworkInterface类。
Querying interface status can be done using the NetworkInterface.GetIsNetworkAvailable()
method call. 可以使用NetworkInterface.GetIsNetworkAvailable()
方法调用来完成查询接口状态的操作。
Example: 例:
var adapters = NetworkInterface.GetAllNetworkInterfaces();
if (adapters != null && adapters.Length > 0)
{
foreach (NetworkInterface adapter in adapters)
{
Console.WriteLine("Network interface {0} is {1}",
adapter.Name,
(NetworkInterface.GetIsNetworkAvailable()?"up":"down")
);
}
}
The above code will yield results as: 上面的代码将产生如下结果:
Network interface Local Area Network is up 网络接口局域网已启动
Network interface Loopback Pseudo-Interface 1 is up 网络接口环回伪接口 1已启动
Now you can encapsulate this into your property's getter for instance you just need to add filtering for the adapters name. 现在,您可以将其封装到属性的getter中,例如,您只需要为适配器名称添加过滤即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.