MVP? MVP!

i love C#
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

执行外部程序并获得返回值(转)

Posted on 2008-09-18 10:50  renhb  阅读(285)  评论(0)    收藏  举报
Process p = new Process();
            p.StartInfo.FileName
= @"C:\windows\system32\command.com";
            p.StartInfo.UseShellExecute
= false;
            p.StartInfo.RedirectStandardInput
= true; p.StartInfo.RedirectStandardOutput = true;

            p.Start();

            p.StandardInput.WriteLine(
"dir");
           
string s = p.StandardOutput.ReadToEnd();

            Console.WriteLine(s);

            Console.ReadKey();

程序的输出为:
Volume in drive E is Data
Volume Serial Number is 9672-0439

Directory of E:\Project\CTest2\CTest2\bin\Debug

2008-09-17  15:44    <DIR>          .
2008-09-17  15:44    <DIR>          ..
2008-09-18  09:48            5,120 CTest2.exe
2008-09-18  09:48            13,824 CTest2.pdb
2008-09-18  09:48            14,328 CTest2.vshost.exe
              3 File(s)        33,272 bytes
              2 Dir(s)  16,306,057,216 bytes free