C#测试网络链接状态
网络判断网络连接有多种办法,通过C#程序也可以判断与远程主机的连接状态。具体实现方法public string NetStatus() { 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 ""; } }
本文来自博客园,作者:陈国利,转载请注明原文链接:https://www.cnblogs.com/cgli/archive/2011/01/18/1938391.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
如果您觉得文章对您有帮助,可以点击文章右下角"推荐"或关注本人博客。您的鼓励是作者坚持原创和持续写作的最大动力!