怎么样判断网络通不通啊?

解决方案 »

  1.   

    //先通过.Net提供的类库判断似乎否有网络连接
            public static bool IsAnyNetworkAvailable()
            {
                try
                {
                    foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
                    {
                        if (ni.OperationalStatus == OperationalStatus.Up &&
                            ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
                            ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
                        {
                            IPInterfaceProperties iip = ni.GetIPProperties();
                            if (iip != null && iip.UnicastAddresses != null && iip.UnicastAddresses.Count > 0)
                            {
                                return true;
                            }
                        }
                    }
                    return false;
                }
                catch
                {
                    //如果有异常,直接调用API判断
                    return IsInternetConnected();
                }
            }        [DllImport("wininet.dll")]
            private static extern bool InternetGetConnectedState(out int connectionDescription, int reservedValue);        public static bool IsInternetConnected()
            {
                int i;
                return InternetGetConnectedState(out i, 0);
            }
      

  2.   

    用socket类中的tcpclient连接一下外网某个地址,看能不能连通...当然,这是我的笨办法.
      

  3.   

    1你要判断网卡 是否连线~~
     if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
                {
                  
                    System.Net.NetworkInformation.NetworkInterface[] _NetFace = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
                    foreach (System.Net.NetworkInformation.NetworkInterface _OneNet in _NetFace)
                    {
                        MessageBox.Show(_OneNet.OperationalStatus.ToString());
                    }            }
                
     2能不能访问外面 你直接socket连接外面的地址就好了
      

  4.   

    1你要判断网卡 是否连线~~
     if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
                {
                  
                    System.Net.NetworkInformation.NetworkInterface[] _NetFace = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
                    foreach (System.Net.NetworkInformation.NetworkInterface _OneNet in _NetFace)
                    {
                        MessageBox.Show(_OneNet.OperationalStatus.ToString());
                    }            }
                
     2能不能访问外面 你直接socket连接外面的地址就好了
      

  5.   

    用c# ping
    using System;using System.Diagnostics;namespace ZZ{     class ZZConsole     {         [STAThread]         static void Main(string[] args)         {                  string ip = "192.192.132.229";              string strRst = CmdPing(ip);              Console.WriteLine(strRst);              Console.ReadLine();         }         private static string CmdPing(string strIp)         {              Process p = new Process();              p.StartInfo.FileName = "cmd.exe";              p.StartInfo.UseShellExecute = false;              p.StartInfo.RedirectStandardInput = true;              p.StartInfo.RedirectStandardOutput = true;              p.StartInfo.RedirectStandardError = true;              p.StartInfo.CreateNoWindow = true;              string pingrst;              p.Start();              p.StandardInput.WriteLine("ping -n 1 "+strIp);              p.StandardInput.WriteLine("exit");              string strRst = p.StandardOutput.ReadToEnd();              if(strRst.IndexOf("(0% loss)")!=-1)                   pingrst = "连接";              else if( strRst.IndexOf("Destination host unreachable.")!=-1)                   pingrst = "无法到达目的主机";              else if(strRst.IndexOf("Request timed out.")!=-1)                   pingrst = "超时";              else if(strRst.IndexOf("Unknown host")!=-1)                   pingrst = "无法解析主机";              else                   pingrst = strRst;              p.Close();              return pingrst;         }     }}
      

  6.   

    [DllImport("wininet.dll")]
    请问这句话要写在哪啊?怎么提示我说: 找不到类型或命名空间名称“DllImport”(是否缺少 using 指令或程序集引用?)
      

  7.   


    using System.Runtime.InteropServices;就当函数来写啊
      

  8.   


    你这还不如用CMD ping 啊 
      

  9.   

    using System.Runtime.InteropServices; 
    namespace internet 
    { public class Class1 

    [DllImport("wininet.dll")] 
    private extern static bool InternetGetConnectedState( out int connectionDescription, int     reservedValue ) ; 
    public Class1(){} 
    private bool IsConnected() 

    int I=0; 
    bool state = InternetGetConnectedState(out I,0); 
    return state; 

    } }