class Program { public string cmdPing(string strIP) { Process myProcess = new Process(); myProcess.StartInfo.FileName = "cmd.exe"; myProcess.StartInfo.UseShellExecute = false; //要重定向 IO 流,Process 对象必须将 UseShellExecute 属性设置为 False。 myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardError = true;
string pingstr; myProcess.Start();
myProcess.StandardInput.WriteLine("ping " + strIP); myProcess.StandardInput.WriteLine("exit");
string strRst = myProcess.StandardOutput.ReadToEnd(); if (strRst.IndexOf("(0% loss)") != -1) pingstr = "连接"; else if (strRst.IndexOf("Destination host unreachable.") != -1) pingstr = "无法到达主机"; else if (strRst.IndexOf("UnKonw host") != -1) pingstr = "无法解析主机"; else pingstr = strRst; myProcess.Close(); return pingstr; } static void Main(string[] args) { Program myProgram = new Program(); string returnString = myProgram.cmdPing("127.0.0.1"); Console.WriteLine(returnString); Console.ReadLine(); } } |
Ping 程序C#实现方法一
最新推荐文章于 2013-08-12 09:58:05 发布