外网IP当本地连接断开时如何控制WebClient client = new WebClient();
            byte[] bytRecv = client.DownloadData("http://www.eyesoft.com.cn/ip.asp"); //下載數據
            string str = System.Text.Encoding.GetEncoding("gb2312").GetString(bytRecv);
            string r = @"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))$";
            string ip = System.Text.RegularExpressions.Regex.Match(str, r).ToString();   //提取信息
             this.label6.Text = ip.ToString();这段是获取外网IP的代码,请各位大虾帮帮忙,如果本地连接断开的时候怎么控制呢?

解决方案 »

  1.   

    用Try-Catch捕获这个错误,再处理!
      

  2.   

    try
    {
    WebClient client = new WebClient();
    byte[] bytRecv = client.DownloadData("http://www.eyesoft.com.cn/ip.asp"); //下載數據
    string str = System.Text.Encoding.GetEncoding("gb2312").GetString(bytRecv);
    string r = @"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))$";
    string ip = System.Text.RegularExpressions.Regex.Match(str, r).ToString(); //提取信息
    this.label6.Text = ip.ToString();
    }
    catch
    {
    //这里放错误处理代码
    }