Heyn

在编程中寻找快乐

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

题记:在自己的程序中调用CMD执行某个命令,将结果输出到某文件保存,以供使用

 

两种方法:

1、调用CMD后逐行读取写入文件

2、使用CMD的重定向输出命令

 

View Code
  1 using System;
  2  using System.Collections.Generic;
  3  using System.Linq;
  4  using System.Text;
  5  using System.Diagnostics;
  6  
  7  namespace aboutls
  8  {
  9      internal class DosCommandDo
 10      {        
 11          public static void Execute(string dosCommand, string fileName, int milliseconds)
 12          {
 13              if (dosCommand != null && dosCommand != "")
 14              {   
 15                  //TODO judgement the fileName
 16                  Process process = new Process();
 17                  ProcessStartInfo startInfo = new ProcessStartInfo();
 18                  startInfo.FileName = "cmd.exe";
 19                  startInfo.Arguments = "/c " + dosCommand;
 20                  startInfo.UseShellExecute = false;
 21                  startInfo.RedirectStandardInput = false;
 22                  startInfo.RedirectStandardOutput = true;
 23                  startInfo.CreateNoWindow = true;
 24                  process.StartInfo = startInfo;
 25  
 26                  try
 27                  {
 28                      if (process.Start())
 29                      {
 30                          if (milliseconds == 0)                        
 31                              process.WaitForExit();                        
 32                          else                        
 33                              process.WaitForExit(milliseconds);
 34                              
 35                          System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName,
 36                              false, System.Text.Encoding.GetEncoding("gb2312"));
 37                          System.IO.StreamReader reader = process.StandardOutput;//截取输出流
 38                          string line = reader.ReadLine();//每次读取一行
 39                          sw.WriteLine(line);
 40                          sw.Flush();
 41  
 42                          while (!reader.EndOfStream)
 43                          {
 44                              line = reader.ReadLine();
 45                              sw.WriteLine(line);
 46                              sw.Flush();
 47                          }
 48  
 49                          if (reader != null)                        
 50                              reader.Close();
 51                          if (sw != null)
 52                              sw.Close(); 
 53                      }
 54                  }
 55                  catch (Exception e)
 56                  {
 57                      Console.WriteLine(e.Message);
 58                  }
 59                  finally
 60                  {
 61                      if (process != null)                    
 62                          process.Close();
 63                  }
 64              }       
 65          }
 66  
 67          public static void ExecuteOut(string dosCommand, string fileName, int milliseconds)
 68          {
 69              if (dosCommand != null && dosCommand != "")
 70              {
 71                  Process process = new Process();
 72                  ProcessStartInfo startInfo = new ProcessStartInfo();
 73                  startInfo.FileName = "cmd.exe";
 74                  startInfo.Arguments = "/c " + dosCommand + " > "+fileName;
 75                  startInfo.UseShellExecute = false;
 76                  startInfo.RedirectStandardInput = false;
 77                  startInfo.RedirectStandardOutput = true;
 78                  startInfo.CreateNoWindow = true;
 79                  process.StartInfo = startInfo;
 80  
 81                  try
 82                  {
 83                      if (process.Start())
 84                      {
 85                          if (milliseconds == 0)
 86                              process.WaitForExit();
 87                          else
 88                              process.WaitForExit(milliseconds);
 89                      }
 90                  }
 91                  catch (Exception e)
 92                  {
 93                      Console.WriteLine(e.Message);
 94                  }
 95                  finally
 96                  {
 97                      if (process != null)
 98                      
 99                          process.Close();
100                  }
101              }
102          }
103      }
104  }

 

posted on 2012-09-22 15:58  Heyn  阅读(755)  评论(0)    收藏  举报