gpt4 book ai didi

c# - 运行进程openfiles输出结果到文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:37 25 4
gpt4 key购买 nike

我正在尝试将 openfiles 过程的结果输出到一个文件,但我没有得到任何结果,有人可以解释为什么吗?我尝试了两种不同的方法。如果我使用命令提示符并运行相同的进程,它会在我的文件中显示结果。

更新:我添加了第三种方法,更改为 redirectstandardoutput = true我试过了,现在我得到了一个文件,但没有结果

更新:我发现问题是在 64 位系统上执行此操作时将构建选项设置为 x86 我认为它正在运行 32 位版本的 openfiles。我通过运行我的应用程序并使用我应该首先做的 RedirectStandardError 流进行测试:) 这就是它所说的“错误:目标系统必须运行 32 位操作系统。”

//First Method
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "openfiles";
proc.StartInfo.Arguments = "/query /FO CSV /v > " + "\"" + Application.StartupPath + @"\OpenFiles.log" + "\"";
proc.Start();
}

//Second method
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "cmd";
proc.StartInfo.Arguments = "/C openfiles /query /FO CSV /v > " + "\"" + Application.StartupPath + @"\OpenFiles.log" + "\"";
proc.Start();
}

//Third method
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "openfiles";
proc.StartInfo.Arguments = "/query /FO CSV /v";
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
if (output != null)
File.WriteAllText(Path.Combine(Application.StartupPath, "OpenFiles.log"), output);
}

最佳答案

重定向不会像那样工作,因为 > filename 不被视为“参数”。

通常,您将捕获应用程序中的输出并自行将其写入文件:

proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

File.WriteAllText(Path.Combine(Application.StartupPath, "OpenFiles.log"), output);

关于c# - 运行进程openfiles输出结果到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26022720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com