日期:2014-05-18  浏览次数:20883 次

如果检测网卡是否插了网线?
这个方法是检测当前状态是否能连上互联网,但如果我要访问的网站是本地网,而非互联网,则怎么使用?
  [DllImport("wininet.dll")]  
  private extern static bool InternetGetConnectedState(ref uint lpdwFlags, uint dwReserved);  
  /// <summary>
  /// 检查互联网联接状态
  /// </summary>
  /// <returns>返回是否与服务器联通状态</returns>
  public bool IsConnectedByServer()
  {
  uint connectionDescription = 0x0;
  bool isConnected = false;
  isConnected = InternetGetConnectedState(ref connectionDescription, 0);
  return isConnected;
  }


------解决方案--------------------
没尝试过
=待高手的指点
------解决方案--------------------
我也想了解,谢谢LZ.
------解决方案--------------------
楼主可以试一下PING一些网站啊,在.net里执行DOS命令..以前试过.可以的.
------解决方案--------------------
网卡灯亮了说明通了.
------解决方案--------------------
C# code
NetworkInterface.GetIsNetworkAvailable()

------解决方案--------------------
ping一下不就好了
------解决方案--------------------
C# code

        private static ManagementClass mc;
        private static ManagementObjectCollection moc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                moc = mc.GetInstances();

                foreach (ManagementObject mo in moc) //查找网卡配置
                {
string s = ((string)mo["Caption"]);

                        if (s == NIC)
                        {
                            if ((bool)mo["DHCPEnabled"])  //此网卡未配置过IP
                            {
                                string[] ips = new string[1] { LocalIP };
                                string[] masks = new string[1] { StationIPmask };
                                Object[] objs = new object[2] { ips, masks };
                                mo.InvokeMethod("EnableStatic", objs);
                                NICValid = false;
                                return;
                            }

                            string[] str = (string[])mo["IPAddress"]; //多个ip地址

                            if (str.Length >= 3)
                            {
                                InfoString = "不能有多个IP地址绑定在此网卡上!";
                                NICValid = false;
                                return;
                            }

                            if (str.Length < 0)
                            {
                                InfoString = "请检查网卡设备!";
                                NICValid = false;
                                return;
                            }

                            if (str[0] == "0.0.0.0")
                            {
                                InfoString = "请检查网卡与交换机正确连接!";
                                NICValid = false;
                                return;
                            }
}