2010-09-30 99 views

回答

0

查看MSDN代碼,http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.aspx。它顯示如何查詢網絡適配器以確定其狀態。

public class NetworkingExample 
{ 
    public static void Main() 
    { 
     NetworkChange.NetworkAddressChanged += new 
     NetworkAddressChangedEventHandler(AddressChangedCallback); 
     Console.WriteLine("Listening for address changes. Press any key to exit."); 
     Console.ReadLine(); 
    } 
    static void AddressChangedCallback(object sender, EventArgs e) 
    { 

     NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); 
     foreach(NetworkInterface n in adapters) 
     { 
      Console.WriteLine(" {0} is {1}", n.Name, n.OperationalStatus); 
     } 
    } 
} 
+0

實際上,有一個更簡單的方法:NetworkInterface.GetIsNetworkAvailable(); – quzui 2010-09-30 11:18:22

+0

它不需要通過適配器循環... – quzui 2010-09-30 11:18:41

1

您可能正在尋找NetworkInterface類。

相關問題