使用C#调用外部命令

        Process Main_P = new Process();
  Main_P.StartInfo.FileName = @"C:\\rmc.exe";//运行的文件
  Main_P.StartInfo.Arguments = @"-screefull";//参数
  Main_P.StartInfo.UseShellExecute = true;
  Main_P.Start();

<转>
使用C#调用外部命令
首先,我们用使用Process类,来创建独立的进程,导入System.Diagnostics,
uing 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 -t  192.192.132.229");
p.StandardInput.WriteLine("exit");
从输出流获取命令执行结果,
string strRst = p.StandardOutput.ReadToEnd();
////////////////////////////////////////////////////////////////////////////


posted @ 2008-01-25 10:26  感觉De味道  阅读(1795)  评论(1编辑  收藏  举报