C#与.BAT错误和放文件执行;输出重定向 [英] C# .bat file execution with Error & Output redirection

查看:145
本文介绍了C#与.BAT错误和放文件执行;输出重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行一个.bat文件。

里面那个的.bat 文件,在它的末端是code

 启动_file_creator.bat%some_arg_name%
ENDLOCAL
出口

我不希望显示在执行过程中的窗口,也是我必须等待,直到完成该做的的.bat 文件的操作,然后终止执行(在操作结束时,我看到了标准文本preSS任意键继续)。我也需要通过检查该文件的输出和错误,所以我尝试使用code:

 的System.Diagnostics.Process PROC =新的System.Diagnostics.Process();
        proc.StartInfo.FileName = @C:\\ m_f \\ _config.bat
        proc.StartInfo.RedirectStandardError = TRUE;
        proc.StartInfo.RedirectStandardOutput =真;
        proc.StartInfo.UseShellExecute = FALSE;
        proc.StartInfo.CreateNoWindow = TRUE;        proc.Start();
        proc.WaitForExit();
        输出1 = proc.StandardError.ReadToEnd();
        proc.WaitForExit();
        输出2 = proc.StandardOutput.ReadToEnd();
        proc.WaitForExit();

但我得到的是错误

  Windows无法找到文件_file_creator.bat。
请确保您正确,键入名称,然后再试一次。

当然,如果我运行的.bat proc.StartInfo.UseShellExecute = TRUE 它工作正常文件,但在这种情况下,我不能设置 RedirectStandardError = TRUE RedirectStandardOutput = TRUE

如何解决?

修改

使用code现在的工作。

  proc.StartInfo.FileName = @C:\\ m_f \\ _config.bat
 proc.StartInfo.WorkingDirectory = @C:\\ m_f \\;


解决方案

尝试正确设置工作目录,或确保 _file_creator.bat 是<$ C某处$ C> PATH 。请参阅<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx\"相对=nofollow>关于结合工作目录文档 UseShellExecute


  

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx\"相对=nofollow>工作目录属性表现的不同取决于在 UseShellExecute 属性的值。当 UseShellExecute 真正的<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx\"相对=nofollow>工作目录属性指定可执行文件的位置。如果<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx\"相对=nofollow>工作目录是一个空字符串,则假定当前目录包含可执行文件。


  
  

UseShellExecute 的<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx\"相对=nofollow>工作目录财产不用于查找可执行文件。相反,它仅由被启动,并仅在新进程的上下文中的意思的方法中使用。当 UseShellExecute 的的文件名属性必须是可执行文件的完全限定路径。


I have a .bat file to be executed.

Inside that .bat file, at its end is that code

START _file_creator.bat %some_arg_name%
ENDLOCAL
EXIT

I don't want to show the window during the execution and also I must wait until the operation doing by this .bat file is completed and then terminate the execution (at the end of the operation I see the standard text "Press any key to continue"). I need also to check the output and errors by that file, so I'm trying to use that code:

        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = @"C:\m_f\_config.bat";
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = true;

        proc.Start();
        proc.WaitForExit();
        output1 = proc.StandardError.ReadToEnd();
        proc.WaitForExit();
        output2 = proc.StandardOutput.ReadToEnd();
        proc.WaitForExit();

But all I get is the error

Windows can not find file "_file_creator.bat".
Make sure you typed the name correctly and try again.

of course if I run that .bat file with the proc.StartInfo.UseShellExecute = true it works fine, but in that case I can't set RedirectStandardError = true and RedirectStandardOutput = true

How to fix it ?

Edit

using that code it works now

 proc.StartInfo.FileName = @"C:\m_f\_config.bat";
 proc.StartInfo.WorkingDirectory = @"C:\m_f\";

解决方案

Try setting the working directory correctly or make sure that _file_creator.bat is somewhere in the PATH. See the documentation about the working directory in conjunction with UseShellExecute:

The WorkingDirectory property behaves differently depending on the value of the UseShellExecute property. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, it is assumed that the current directory contains the executable.

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used only by the process that is started and has meaning only within the context of the new process. When UseShellExecute is false, the FileName property must be a fully qualified path to the executable.

这篇关于C#与.BAT错误和放文件执行;输出重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆