简体   繁体   中英

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.

Querying interface status can be done using the NetworkInterface.GetIsNetworkAvailable() method call.

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

Now you can encapsulate this into your property's getter for instance you just need to add filtering for the adapters name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM