c# 网络验证

 

#region 检查网络连接
 
       static bool exitResult = false;
       /// <summary>
       /// 检查网络连接
       /// add by wlzhang 2014年1月09日 21:33:40
       /// </summary>
       /// <returns>true为已连接,false为断开</returns>
       public static ResultMessage<bool, string> CheckInternetConnect()
       {
           ResultMessage<bool, string> result = new ResultMessage<bool, string>(false, "请检查网络状态...");
           Thread checkNetThread = new Thread(new ThreadStart(CheckNetState));
           checkNetThread.Start();
           checkNetThread.Join(5000);
           if (exitResult)
           {
               result = new ResultMessage<bool, string>(true, "");
           }
           return result;
       }
       /// <summary>
       /// 检查外网连接
       /// </summary>
       public static void CheckNetState()
       {
           if (IsConnected())
           {
               try
               {
                   Ping p = new Ping();
                   PingReply pr = p.Send("dev.voicecloud.cn");
                   if (pr.Status == IPStatus.Success)
                   {
                       exitResult = true;
                   }
                   else
                   {
                       exitResult = false;
                   }
               }
               catch (Exception e)
               {
                   e.GetBaseException();
                   exitResult = false;
               }
           }
           else
           {
               exitResult = false;
           }
       }
 
       [DllImport("wininet.dll")]
       private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
       /// <summary>
       /// 检查内网连接
       /// </summary>
       /// <returns>true为已连接,false为断开</returns>
       private static bool IsConnected()
       {
           int I = 0;
           bool state = InternetGetConnectedState(out I, 0);
           return state;
       }
 
       #endregion

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Manage
{
    public class ResultMessage<T1, T2>
    {
        private T1 _t1;

        private T2 _t2;

        public T1 Result
        {
            get
            {
                return _t1;
            }
            set
            {
                _t1 = value;
            }
        }

        public T2 Message
        {
            get
            {
                return _t2;
            }
            set
            {
                _t2 = value;
            }
        }

        public ResultMessage(T1 result, T2 message)
        {
            _t1 = result;
            _t2 = message;
        }
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。