c#檢測網路連接(主要是區域網路)

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   io   art   問題   

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 = "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)            {      & 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.