C#与.BAT错误和放文件执行;输出重定向 [英] C# .bat file execution with Error & Output redirection
问题描述
我要执行一个.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 codeSTART _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 theproc.StartInfo.UseShellExecute = true
it works fine, but in that case I can't setRedirectStandardError = true
andRedirectStandardOutput = 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 thePATH
. See the documentation about the working directory in conjunction withUseShellExecute
: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屋!