C#中利用process类调用外部程序以及执行dos命令

C#中利用process类调用外部程序以及执行dos命令

文章类别:C#应用 | 发表日期:2006-9-13

<script language="JavaScript" src="http://img.knowsky.com/img/wangzhai/wangzhai.js" type="text/javascript"></script> 新浪ViVi 搜狐网摘 365Key网摘 天极网摘 和讯网摘 POCO网摘 YouNote网摘 博拉网 天下图摘 </script> marginwidth="0" marginheight="0" src="http://cpro.baidu.com/cpro/ui/uijs.php?&uid=2214&n=sayyescpr&tm=45&cm=45&um=22&rsi0=300&rsi1=280&wn=1&hn=7&rssl0=right&rssl1=bottom&rsi2=0&rss0=%23trans&rss1=%23trans&rss2=%23E10900&rss3=%23444444&rss4=%23008000&rss5=%23000000&rad=1&word=http%3A%2F%2Fwww.knowsky.com%2F340583.html&fv=9&cn=2&refer=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26q%3Dc%2B%25E8%25B0%2583%25E7%2594%25A8%25E5%25A4%2596%25E9%2583%25A8%25E7%25A8%258B%25E5%25BA%258F%26meta%3D%26aq%3Dt%26oq%3Dc%2523%2B%25E8%25B0%2583%25E7%2594%25A8" frameborder="0" width="300" scrolling="no" height="280" allowtransparency="allowtransparency">

c#中的Process类可方便的调用外部程序,所以我们可以通过调用cmd.exe程序

加入参数 "/c " + 要执行的命令来执行一个dos命令
(/c代表执行参数指定的命令后关闭cmd.exe /k参数则不关闭cmd.exe)

 1         private string RunCmd(string command)
     2         {
 3             //實例一個Process類,啟動一個獨立進程
 4             Process p = new Process();
 5
 6             //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:
 7
 8             p.StartInfo.FileName = "cmd.exe";           //設定程序名
 9             p.StartInfo.Arguments = "/c " + command;    //設定程式執行參數
10             p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
11             p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
12             p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
13             p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
14             p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口
15
16             p.Start();   //啟動
17            
18             //p.StandardInput.WriteLine(command);       //也可以用這種方式輸入要執行的命令
19             //p.StandardInput.WriteLine("exit");        //不過要記得加上Exit要不然下一行程式執行的時候會當機
20            
21             return p.StandardOutput.ReadToEnd();        //從輸出流取得命令執行結果
22
23         }
http://www.cnblogs.com/coderlee/archive/2006/09/12/502433.html

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值