首页 > 开发 > 综合 > 正文

在C#中使用Ping

2024-07-21 02:26:12
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!

using system;
using system.diagnostics;

namespace consoleapplication7 {
 class class1 {

  [stathread]
  static void main(string[] args) {   
   string ip = "10.10.40.31";
   string strrst = cmdping(ip);
   console.writeline(strrst);
   console.readline();
  }

  private static string cmdping(string strip) {
   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;
  }
 }
}

上一篇:C#对话框

下一篇:C#还原数据库

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表