Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#.bat文件执行时出错&;输出重定向_C#_Batch File_Exe - Fatal编程技术网

C#.bat文件执行时出错&;输出重定向

C#.bat文件执行时出错&;输出重定向,c#,batch-file,exe,C#,Batch File,Exe,我有一个.bat文件要执行 在.bat文件中,在其末尾是该代码 START _file_creator.bat %some_arg_name% ENDLOCAL EXIT 我不想在执行过程中显示窗口,而且我必须等待此.bat文件执行的操作完成,然后终止执行(在操作结束时,我看到标准文本“按任意键继续”)。我还需要检查该文件的输出和错误,因此我尝试使用该代码: System.Diagnostics.Process proc = new System.Diagnostics.Pro

我有一个.bat文件要执行

.bat
文件中,在其末尾是该代码

START _file_creator.bat %some_arg_name%
ENDLOCAL
EXIT
我不想在执行过程中显示窗口,而且我必须等待此
.bat
文件执行的操作完成,然后终止执行(在操作结束时,我看到标准文本“按任意键继续”)。我还需要检查该文件的输出和错误,因此我尝试使用该代码:

        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();
但我得到的只是错误

Windows can not find file "_file_creator.bat".
Make sure you typed the name correctly and try again.
当然,如果我用
proc.StartInfo.UseShellExecute=true
运行
.bat
文件,它可以正常工作,但在这种情况下,我无法设置
RedirectStandardError=true
RedirectStandardOutput=true

如何修复它

编辑 使用该代码,它现在可以工作了

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

请尝试正确设置工作目录,或确保
\u file\u creator.bat
位于
路径中的某个位置。请参阅与
UseShellExecute
结合使用的关于工作目录的说明:

根据UseShellExecute属性的值,该属性的行为会有所不同。当UseShellExecutetrue时,该属性指定可执行文件的位置。如果是空字符串,则假定当前目录包含可执行文件

UseShellExecutefalse时,该属性不用于查找可执行文件。相反,它仅由已启动的流程使用,并且仅在新流程的上下文中具有意义。当UseShellExecutefalse时,属性必须是可执行文件的完全限定路径


_文件_creator.bat文件与已执行的.bat文件位于同一目录中,这无关紧要。工作目录是从您的C#程序继承的,如上所述。您必须自己为新进程设置工作目录,才能使其工作。