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 "";
                        }
                }






posted on 2011-01-18 15:35  陈国利  阅读(2160)  评论(0编辑  收藏  举报