21. p.StartInfo.RedirectStandardOutput = true;
22. p.StartInfo.RedirectStandardError = true;
23. p.StartInfo.CreateNoWindow = true;
24. string pingrst;
25. p.Start();
26. p.StandardInput.WriteLine("ping -n 1 "+strIp);
27. p.StandardInput.WriteLine("exit");
28. string strRst = p.StandardOutput.ReadToEnd();
29. if(strRst.IndexOf("(0% loss)")!=-1)
30. pingrst = "连接";
31. else if( strRst.IndexOf("Destination host unreachable.")!=-1)
32. pingrst = "无法到达目的主机";
33. else if(strRst.IndexOf("Request timed out.")!=-1)
34. pingrst = "超时";
35. else if(strRst.IndexOf("Unknown host")!=-1)
36. pingrst = "无法解析主机";
37. else
38. pingrst = strRst;
39. p.Close();
40. return pingrst;
41. }
42. }
43. }
总结,这里就是为了说明一个问题,不但是Ping命令,只要是命令行程序或者是Dos内部命令,我们都可以用上面的方式来执行它,并获取相应的结果,并且这些程序的执行过程不会显示出来,如果需要调用外部程序就可以嵌入到其中使用了。
其余方法:
方法1
下面这个程序就是,你可以直接去建立一个控制台应用程序去调试一下
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C# 网络连接判断(3)在线全文阅读。
相关推荐: