[英]How to detect the state of a network interface whether it is enabled?
如何檢測網絡接口是否啟用/禁用?
public Boolean IsNicEnabled(String nic) { return true; }
您可以查看有關此內容的System.Net.NetworkInformation.NetworkInterface類。
可以使用NetworkInterface.GetIsNetworkAvailable()
方法調用來完成查詢接口狀態的操作。
例:
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")
);
}
}
上面的代碼將產生如下結果:
網絡接口局域網已啟動
網絡接口環回偽接口 1已啟動
現在,您可以將其封裝到屬性的getter中,例如,您只需要為適配器名稱添加過濾即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.