码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-04 16:55:57      阅读:230      评论:0      收藏:0      [点我收藏+]

标签: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 = "无法解析主机";

else

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

 

c#检测网络连接(主要是局域网),布布扣,bubuko.com

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

标签:des   style   blog   color   os   io   art   问题   

原文地址:http://www.cnblogs.com/ruishuang208/p/3890313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!