教研室

标题: .net 判断是否连接上网络 [打印本页]

作者: 教研室    时间: 2011-8-13 22:19:37     标题: .net 判断是否连接上网络

/// ManagementObjectSearcher
/// </summary>
/// <returns></returns>
public static string GetState()
{
//using System.Management;
ManagementObjectCollection objects;
string status = "";
ManagementObjectSearcher searcher = new ManagementObjectSearcher();
searcher.Query.QueryString = "Select * From Win32_NetworkAdapter";
objects = searcher.Get();
foreach (ManagementObject obj in objects)
{
foreach (PropertyData p in obj.Properties)
{
if (p.Name.Equals("NetConnectionStatus"))
{
if (p.Value != null)
{
status = p.Value.ToString();
}
}
}
}
switch (status)
{
case "0":
return "Disconnected";
case "1":
return "Connecting ...";
case "2":
return "Connected";
case "3":
return "Disconnecting ...";
case "4":
return "Hardware not present";
case "5":
return "Hardware disabled";
case "6":
return "Hardware malfunction";
case "7":
return "Media disconnected";
case "8":
return "Authenticating";
case "9":
return "Authentication succeeded";
case "10":
return "Authentication failed";
default:
return "";
}
}


}
}




欢迎光临 教研室 (http://www.jiaoyanshi.com/) Powered by Discuz! X2