c# - 批处理文件的执行不符合 C# 中的预期

标签 c# batch-file batch-processing

我在 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/

相关文章:

c# - 以编程方式安装 VSTO 加载项

batch-file - 创建批处理文件以删除特定文件

visual-studio-code - 如何在 VS Code 中快速关闭所有打开的终端?

linux - 使用 slurm 作业 ID

c# - 使用 IDistributedCache 列出和删除给定组中的所有缓存条目

c# - 调试 C++/Cli : <Unknown function> and no Locals

c# - 如何通过Linq从xml中获取值

batch-file - 如何在 FFmpeg 的参数中使用六十进制语法,或者如果不可能,如何在 Windows CMD shell 中转换它

c# - 在 Windows 8 中执行自动更改连接的显示器

Java批处理作业监听器afterJob获取作业的id