如何用C#编写一个像CMD的程序,要求能够ping一个主机并且返回消息就行!
请帮忙给我说一下如何编写一个控制台程序,只要有ping的功能就行,其余的我可以自己联想,现在没有一个思路...
请帮忙给我说一下如何编写一个控制台程序,只要有ping的功能就行,其余的我可以自己联想,现在没有一个思路
展开
2个回答
展开全部
try
{
Ping PingInfo = new Ping();
PingOptions PingOpt = new PingOptions();
PingOpt.DontFragment = true;
string myInfo = "hyworkhyworkhyworkhyworkhyworkhywork";
byte[] bufferInfo = Encoding.ASCII.GetBytes(myInfo);
int TimeOut = 120;
PingReply reply = PingInfo.Send(this.textBox1.Text, TimeOut, bufferInfo, PingOpt);
if (reply.Status == IPStatus.Success)
{
this.textBox2.Text = reply.RoundtripTime.ToString();
this.textBox3.Text = reply.Options.Ttl.ToString();
this.textBox4.Text = (reply.Options.DontFragment ? "发生分段" : "没有发生分段");
this.textBox5.Text = reply.Buffer.Length.ToString();
}
else
{
MessageBox.Show("无法Ping通");
}
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
{
Ping PingInfo = new Ping();
PingOptions PingOpt = new PingOptions();
PingOpt.DontFragment = true;
string myInfo = "hyworkhyworkhyworkhyworkhyworkhywork";
byte[] bufferInfo = Encoding.ASCII.GetBytes(myInfo);
int TimeOut = 120;
PingReply reply = PingInfo.Send(this.textBox1.Text, TimeOut, bufferInfo, PingOpt);
if (reply.Status == IPStatus.Success)
{
this.textBox2.Text = reply.RoundtripTime.ToString();
this.textBox3.Text = reply.Options.Ttl.ToString();
this.textBox4.Text = (reply.Options.DontFragment ? "发生分段" : "没有发生分段");
this.textBox5.Text = reply.Buffer.Length.ToString();
}
else
{
MessageBox.Show("无法Ping通");
}
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
展开全部
添加对 Microsoft.VisualBasic.dll 的引用。
Microsoft.VisualBasic.Devices.Network n = new Microsoft.VisualBasic.Devices.Network();
if (n.Ping("192.168.1.1"))
Console.Write("network is ok");
这就是PING 了
Microsoft.VisualBasic.Devices.Network n = new Microsoft.VisualBasic.Devices.Network();
if (n.Ping("192.168.1.1"))
Console.Write("network is ok");
这就是PING 了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询