用C#写一个类似cmd的控制台程序,实现ping的功能!!!(求完整程序源代码!) 10
2个回答
展开全部
using System;
using System.Net;
using System.Net.NetworkInformation;
class Program
{
static void Main(string[] args)
{
int count = 4;
IPAddress addr = IPAddress.Parse("220.181.111.86");
Ping ping = new Ping();
long timeSum = 0;
int succCount = 0;
//发送
Console.WriteLine("正在ping {0}", addr);
for (int i = 0; i < count; ++i)
{
PingReply pr = ping.Send(addr);
if (pr.Status == IPStatus.TimedOut)
Console.WriteLine("超时");
else if (pr.Status == IPStatus.Success)
{
Console.WriteLine("延时:{0}毫秒", pr.RoundtripTime);
++succCount;
timeSum += pr.RoundtripTime;
}
}
if (timeSum != 0)
Console.WriteLine("平均延时{0}毫秒,丢包率{1}%", 1.0 * timeSum / succCount, (count - succCount) * 100.0 / count);
else
Console.WriteLine("丢包率100%");
return;
}
}
展开全部
using System.Diagnostics;
static void Main(string[] args)
{
string ip = "202.108.22.103";
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;
}
}
追问
这个不能用啊?
追答
你有没有加引用,还有你看代码 你把需要输入IP的地方
替换一下stringip ="202.108.22.103";
换成string ip= console.readline();
就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询