C#怎么执行ping命令

发布时间:2021-07-16 18:00:30 作者:chen
来源:亿速云 阅读:370

本篇内容主要讲解“C#怎么执行ping命令”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#怎么执行ping命令”吧!

C#执行ping命令

首先,我们用使用Process类,来创建独立的进程,导入System.Diagnostics,using System.Diagnostics;

实例一个Process类,启动一个独立进程Process p = new Process();

Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法。

下面我们用到了他的几个属性:
◆设定程序名p.StartInfo.FileName = "cmd.exe";
◆关闭Shell的使用p.StartInfo.UseShellExecute = false;
◆重定向标准输入p.StartInfo.RedirectStandardInput = true;
◆重定向标准输出p.StartInfo.RedirectStandardOutput = true;
◆重定向错误输出p.StartInfo.RedirectStandardError = true;
◆设置不显示窗口p.StartInfo.CreateNoWindow = true;

上面几个属性的设置是比较关键的一步。

既然都设置好了那就启动进程吧,p.Start();

C#执行ping命令,输入要执行ping命令,这里就是ping了,
p.StandardInput.WriteLine("ping -n 1 www.iwebtrados.com.cn");
p.StandardInput.WriteLine("exit");

从输出流获取命令执行结果,string strRst = p.StandardOutput.ReadToEnd();

利用C#执行ping命令

这里我写的是一个窗体程序。首先添加textbox,listbox,button控件,其中textbox录入域名或IP,listbox显示结果.

在button1_click事件键入

privatevoidbutton1_Click(objectsender,EventArgse)  {  Pingp1=newPing();//只是演示,没有做错误处理  PingReplyreply=p1.Send(this.textBox1.Text);//阻塞方式  displayReply(reply);//显示结果  }  privatevoiddisplayReply(PingReplyreply)//显示结果  {  StringBuildersbuilder;  if(reply.Status==IPStatus.Success)  {  sbuilder=newStringBuilder();  sbuilder.Append(string.Format("Address:{0}",reply.Address.ToString()));  sbuilder.Append(string.Format("RoundTriptime:{0}",reply.RoundtripTime));  sbuilder.Append(string.Format("Timetolive:{0}",reply.Options.Ttl));  sbuilder.Append(string.Format("Don'tfragment:{0}",reply.Options.DontFragment));  sbuilder.Append(string.Format("Buffersize:{0}",reply.Buffer.Length));  listBox1.Items.Add(sbuilder.ToString());  }  }

也可以做异步的处理,修改button1_click,并添加PingCompletedCallBack方法

privatevoidbutton1_Click(objectsender,EventArgse)  {  Pingp1=newPing();  p1.PingCompleted+=newPingCompletedEventHandler(this.PingCompletedCallBack);  //设置PingCompleted事件处理程序  p1.SendAsync(this.textBox1.Text,null);  }  privatevoidPingCompletedCallBack(objectsender,PingCompletedEventArgse)  {  if(e.Cancelled)  {  listBox1.Items.Add("PingCanncel");  return;  }  if(e.Error!=null)  {  listBox1.Items.Add(e.Error.Message);  return;  }  StringBuildersbuilder;  PingReplyreply=e.Reply;  if(reply.Status==IPStatus.Success)  {  sbuilder=newStringBuilder();  sbuilder.Append(string.Format("Address:{0}",reply.Address.ToString()));  sbuilder.Append(string.Format("RoundTriptime:{0}",reply.RoundtripTime));  sbuilder.Append(string.Format("Timetolive:{0}",reply.Options.Ttl));  sbuilder.Append(string.Format("Don'tfragment:{0}",reply.Options.DontFragment));  sbuilder.Append(string.Format("Buffersize:{0}",reply.Buffer.Length));  listBox1.Items.Add(sbuilder.ToString());  }  }

到此,相信大家对“C#怎么执行ping命令”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. ping命令的基本操作
  2. 使用ping命令检测ip

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:如何用nodejs的http模块创建一个简单的静态资源服务器

下一篇:Web开发中客户端跳转与服务器端跳转有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》