怎么样通过代码来判断电脑能否上网,比如能否打开新浪的网站

解决方案 »

  1.   

    用API可以解决
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
        static int flag;
        static string State = InternetGetConnectedState(out flag, 0).ToString();
        static void Main(string[] args)
        {
            Console.WriteLine(State);
            Console.Read();
        }刚试了一下  ok
    记得要 using System.Runtime.InteropServices;
      

  2.   

    [DllImport("wininet.dll")]
            private extern static bool InternetGetConnectedState(out int ConnectionDescription, int ReservedValue);        public static bool OnlineState()
            {
                int i = 0;
                return InternetGetConnectedState(out i, 0);
            }
      

  3.   

    InternetGetConnectedState 对于 ADSL 好像无法判断.建议枚举拨号连接.
      

  4.   

    ADSL我还真没试过  等高手
      

  5.   

    用socket发送http请求,查看返回信息
      

  6.   

    通过icmp协议编写一个ping程序
      

  7.   

    [Platform SDK: Remote Access Service]
    1.RasEnumConnections 枚举所有拨号连接
    2.RasHangUp 对每个枚举到的连接执行挂起操作(断开连接)
      

  8.   

    RasEnumConnections 枚举所有拨号连接 (如果枚举到的连接数大于 0 ,则表示已连入网络(对于拨号连接入网的电脑,如 ADSL))
      

  9.   

    用System.Net.DNS.GetHostByName() 看是否能返回ip 也可判断 但效率低了点
      

  10.   

    我要一个比较好的代码,是这样,用可能是用无线猫,比如cdma上网,用可能通过无线网卡,有可能是在公司的局域网上网,也用可能是adsl
      

  11.   

    用程序“试”一下最准确了
    用webrequest get 看看服务器返回值就可以了
      

  12.   

    我没有做过,能给个详细信息吗,比如www.163.com
      

  13.   

    用socket发送http请求,查看返回信息赞成这个方法
      

  14.   

    public static bool IsConnectedToInternetUsePing(string URL,int timeOut)
            {
                string pingrst;            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;            p.Start();            p.StandardInput.WriteLine("ping -n 1 -w "+timeOut.ToString()+" " + URL);            p.StandardInput.WriteLine("exit");            string strRst = p.StandardOutput.ReadToEnd();            if(strRst.IndexOf("(0% loss)")!=-1)
                {
                    return true;
                }
                else
                {
                    return false;
                }        }
      

  15.   

    如何用程序启动ie打开一个url网页