首页 > 代码库 > c#检测网络连接(主要是局域网)

c#检测网络连接(主要是局域网)

c#检测网络连接问题我没有看到好的方法,都是通过与外网(或者局域网服务器)传递信息检测的。我看些下下来了代码:   private void button1_Click(object sender, EventArgs e)        {            string ip;            ip = "10.1.148.1";           // string ip = "192.192.132.229";          //  string strRst = CmdPing(ip);         //   MessageBox.Show(strRst);              string   str   =   CmdPingh(ip);                MessageBox.Show(str);          }        private static string CmdPing(string strIp)//方法1{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 = "无法解析主机";elsepingrst = strRst;p.Close();return pingrst;} public   static   string   CmdPingh(string   _strHost)   //与上面的方法一样,不同写法而已  {     string   m_strHost   =   _strHost;                         Process   process   =   new   Process();     process.StartInfo.FileName   =   "cmd.exe";     process.StartInfo.UseShellExecute   =   false;     process.StartInfo.RedirectStandardInput   =   true;     process.StartInfo.RedirectStandardOutput   =   true;     process.StartInfo.RedirectStandardError   =   true;                             process.StartInfo.CreateNoWindow   =   true;     string   pingrst   =   string.Empty;     process.StartInfo.Arguments   =   "ping   "   +   m_strHost   +   "   -n   1";     process.Start();     process.StandardInput.AutoFlush   =   true;     string   temp   =   "ping   "   +   m_strHost   +   "   -n   1"   ;                                             process.StandardInput.WriteLine(temp);                                     process.StandardInput.WriteLine("exit");                             string   strRst   =   process.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;     process.Close();     return   pingrst   ;     } private void button2_Click(object sender, EventArgs e) {     jcip(); }  private void jcip()//方法2{    Ping pingSender = new Ping ();            PingOptions options = new PingOptions ();            // Use the default Ttl value which is 128,            // but change the fragmentation behavior.            options.DontFragment = true;            // Create a buffer of 32 bytes of data to be transmitted.            string data = http://www.mamicode.com/"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";            byte[] buffer = Encoding.ASCII.GetBytes (data);            int timeout = 120;            PingReply reply = pingSender.Send ("10.1.148.1", timeout, buffer, options);            if (reply.Status == IPStatus.Success)            {      &