我正在寻找一种方法,可以识别系统是否连接到网络,或在系统上安装虚拟机时获取本地ip地址。 这是返回的本地IP地址代码:使用c获取本地系统ip地址#
public static string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
return localIP;
}
但是,当系统没有连接到网络时,它会返回其他虚拟机的IP地址。
我也发现这种方法 System.Net.NetworkInformation.NetworkInterface.isconnected
方法可用,但即使网络电缆拔掉,它仍然返回true。
有什么方法可以确定系统是否连接?
也许这可以帮助:http://stackoverflow.com/questions/515436/如何对获得互联网,IP – Feroc 2012-01-03 08:44:16