首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
nizhigang2000 (3)
hongbo781202 (81)
Kendiv (113)
TechnoFantasy (52)
feifei1018 (19)
coofucoo (108)
qingrun (67)
btbtd (83)
longrujun (64)
cyz1980 (101)
CSDN - 文档中心 - .NET 阅读:4503   评论: 10    参与评论
标题   使用C#调用外部Ping命令获取网络连接情况     选择自 zhzuo 的 Blog
关键字   Ping 外部程序
出处  

使用C#调用外部Ping命令获取网络连接情况

 

以前在玩Windows 98的时候,几台电脑连起来,需要测试网络连接是否正常,经常用的一个命令就是Ping.exe。感觉相当实用。

现在 .net为我们提供了强大的功能来调用外部工具,并通过重定向输入、输出获取执行结果,下面就用一个例子来说明调用Ping.exe命令实现网络的检测,希望对.net初学者有所帮助。

首先,我们用使用Process类,来创建独立的进程,导入System.Diagnostics

using System.Diagnostics;

实例一个Process类,启动一个独立进程

Process p = new Process();

Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,

下面我们用到了他的几个属性:

设定程序名

p.StartInfo.FileName = "cmd.exe";

关闭Shell的使用

p.StartInfo.UseShellExecute = false;

重定向标准输入

p.StartInfo.RedirectStandardInput = true;

重定向标准输出

p.StartInfo.RedirectStandardOutput = true;

重定向错误输出

p.StartInfo.RedirectStandardError = true;

设置不显示窗口

p.StartInfo.CreateNoWindow = true;

上面几个属性的设置是比较关键的一步。

既然都设置好了那就启动进程吧,

p.Start();

输入要执行的命令,这里就是ping了,

p.StandardInput.WriteLine("ping -n 1 192.192.132.229");

p.StandardInput.WriteLine("exit");

从输出流获取命令执行结果,

string strRst = p.StandardOutput.ReadToEnd();

在本机测试得到如下结果:

"Microsoft Windows 2000 [Version 5.00.2195]\r\n(C) 版权所有 1985-2000 Microsoft Corp.\r\n\r\nD:\\himuraz\\csharpproject\\ZZ\\ConsoleTest\\bin\\Debug>ping -n 1 192.192.132.231\r\n\r\r\nPinging 192.192.132.231 with 32 bytes of data:\r\r\n\r\r\nReply from 192.192.132.231: bytes=32 time<10ms TTL=128\r\r\n\r\r\nPing statistics for 192.192.132.231:\r\r\n    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),\r\r\nApproximate round trip times in milli-seconds:\r\r\n    Minimum = 0ms, Maximum =  0ms, Average =  0ms\r\r\n\r\nD:\\himuraz\\csharpproject\\ZZ\\ConsoleTest\\bin\\Debug>exit\r\n"

有了输出结果,那还有什么好说的,分析strRst字符串就可以知道网络的连接情况了。 

下面是一个完整的程序,当然对Ping.exe程序执行的结果不全,读者可以进一步修改

完整代码如下:

using System;

using System.Diagnostics;

namespace ZZ

{

     class ZZConsole

     {

         [STAThread]

         static void Main(string[] args)

         {   

              string ip = "192.192.132.229";

              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;

         }

     }

}

 

     总结,这里就是为了说明一个问题,不但是Ping命令,只要是命令行程序或者是Dos内部命令,我们都可以用上面的方式来执行它,并获取相应的结果,并且这些程序的执行过程不会显示出来,如果需要调用外部程序就可以嵌入到其中使用了。

 


相关文章
对该文的评论
jfbao ( 2004-06-20)
thanks
Sunmast ( 2004-05-03)
faint
http://www.csdn.net/Develop/read_article.asp?id=22865
mazifuwhite ( 2004-04-29)
nieyang ( 2004-04-22)
好!!!!
lgqTigerMJ ( 2004-04-18)
啊!好文章,至少它帮我解决了
一个大难问题。  谢谢!!!