从c#读取windows批处理文件的输出

时间:2015-07-08 08:48:30

标签: c# file batch-file

我有一个要求,我必须使用Windows批处理文件检查文件的大小,并从c#代码中读取输出

批处理脚本

@echo off
set maximumbytesize=1000
setlocal EnableDelayedExpansion
for /F  %%a in ('dir /B D:\test_batch_files') do (
  set name=%%a
  set size=%%~za
  if  !size! GTR !maximumbytesize! (
    echo greater
  ) ELSE (
    echo smaller
  )
)

c#代码获取输出:

 Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "D:\\test_batch_files\\GetFileSizeInDirectory.bat";
            //ConfigurationManager.AppSettings["BatchScriptPath"].ToString();                   
            p.Start();
            StreamReader myStreamReader = p.StandardOutput;
             myString = myStreamReader.ReadToEnd();
            p.WaitForExit();

运行批处理的输出应该是(从cmd运行):

smaller
smaller
smaller
greater
smaller
smaller
smaller
smaller
smaller
smaller

c#

的输出
smaller
smaller
smaller
smaller
smaller
smaller
smaller
smaller
smaller
smaller   

0 个答案:

没有答案