如何用C#编写一个像CMD的程序,要求能够ping一个主机并且返回消息就行!
答案:3 悬赏:70 手机版
解决时间 2021-02-06 03:06
- 提问者网友:树红树绿
- 2021-02-05 12:36
请帮忙给我说一下如何编写一个控制台程序,只要有ping的功能就行,其余的我可以自己联想,现在没有一个思路
最佳答案
- 五星知识达人网友:纵马山川剑自提
- 2021-02-05 12:57
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);
}
全部回答
- 1楼网友:低音帝王
- 2021-02-05 14:51
添加对 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 了
- 2楼网友:往事埋风中
- 2021-02-05 14:18
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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯