搜索
查看: 5792|回复: 0
打印 上一主题 下一主题

.net 判断是否连接上网络 [复制链接]

Rank: 9Rank: 9Rank: 9

go
楼主
发表于 2011-8-13 22:19:37 |只看该作者 |倒序浏览
/// 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 "";
}
}


}
}
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0 支持支持0 反对反对0
回顶部