C#调用的PING命令行的程序 有点问题,望高手解决
public static string CmdPing(string strIp)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; //这里换成ping.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 -s "+strIp);//这里换成“-s”
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;
}
上面的代码是调用CMD的命令行的程序,
但是我在修改程序,只调用PING这个命令(如上的注释改的两个地方)。程序运行的时候就不对了。
问题点数:20、回复次数:8Top
1 楼diandian82(点点(nothing))回复于 2006-06-03 17:21:39 得分 0
ping.exe有这个东西么?
我一直以为这是个内部命令。Top
2 楼draren(向上落的雨)回复于 2006-06-03 17:30:11 得分 0
p.Start();
这个语句执行完的时候,ping.exe 已经执行完了。后面的已经没有意义了。Top
3 楼ilove8(千里|你去哪里了,我等了你很久了!)回复于 2006-06-03 17:38:21 得分 0
upTop
4 楼kssys()回复于 2006-06-03 17:40:06 得分 5
public static string GetMAC(string clientip)
{
string mac="";
System.Diagnostics.Process process=new System.Diagnostics.Process();
process.StartInfo.FileName="nbtstat";
process.StartInfo.Arguments="-a "+clientip;
process.StartInfo.UseShellExecute=false;
process.StartInfo.CreateNoWindow=true;
process.StartInfo.RedirectStandardOutput=true;
process.Start();
string output=process.StandardOutput.ReadToEnd();
int length=output.IndexOf("MAC Address =");
if(length>0)
{
mac=output.Substring(length+14,17);
}
return mac;
}Top
5 楼draren(向上落的雨)回复于 2006-06-03 17:45:20 得分 5
Process myProcess = new Process();
try
{
// Get the path that stores user documents.
// string myDocumentsPath =
// Environment.GetFolderPath(Environment.SpecialFolder.Personal);
myProcess.StartInfo.FileName = "ping.exe";
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.UseShellExecute = false;
// myProcess.StartInfo.RedirectStandardInput = true;
// myProcess.StartInfo.RedirectStandardOutput = true;
// myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.Arguments = "-t 127.0.0.1";
myProcess.Start();
}
catch (Win32Exception exp)
{
}
我为了调试方便,把重定向关闭了。Top
6 楼zhzuo(秋枫)回复于 2006-06-04 21:58:52 得分 5
使用C#调用外部Ping命令获取网络连接情况
http://blog.csdn.net/zhzuo/archive/2004/03/21/22024.aspxTop
7 楼jy757443(猫猫要努力)回复于 2006-06-04 22:22:52 得分 5
你应该直接使用 ping 这个类
你这样调用外部网络命令好麻烦啊,我不知道你处于什么样的目的要这样做。
Ping p = new Ping();
p.Send("172.18.0.6").Status;Top
8 楼lookcode()回复于 2006-06-05 03:00:01 得分 0
对,在.NET2.0下就直接有PING这个类,你还用的1.1吧Top




