我在 c:\上有一个批处理文件。当我通过双击执行它时,一切正常。
批处理文件所做的就是这个。
1) 它在同一目录上执行一个 .exe。 2) 将输出重定向到同一目录中的文本文件。
.bat 包含
parse.exe > "temp.txt"
但是,当我通过 C# 执行批处理文件时,根本没有创建 temp.txt。(批处理文件似乎当然可以运行。)
我使用了以下 C# 代码。
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "c://resource//auto.bat";
p.Start();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
我哪里错了? 提前致谢。
编辑
当我把批处理文件改成这样的时候,
parse.exe > "c:\temp.txt"而不是 parse.exe > "temp.txt"
一个临时文件被创建。但是,它不包含 parse.exe 的输出。
最佳答案
process.StartInfo.WorkingDirectory = @"c:\";
您还可以在批处理文件中设置绝对路径,这将立即生效
pathofparse\parse.exe 你的路径\"temp.txt";
关于c# - 批处理文件的执行不符合 C# 中的预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12131205/