C# 使用 Process.Start() 作为 cmd 和活动 RedirectStandardOutput 的参数执行应用程序

C# Execute Application Using Process.Start() as params to cmd and active RedirectStandardOutput

我正在使用 Visual Studio Clickones 并尝试执行 (.appref-ms) application 并使用 RedirectStandardOutput...

我必须使用选项 "Prefer 32-bit" 因为我使用的 Access DB 连接字符串为 Provider=Microsoft.Jet.OLEDB.4.0.

这是我的执行代码:

    var p = new Process();
    p.StartInfo = new ProcessStartInfo(@"C:\Users\hed-b\Desktop\PulserTester.appref-ms")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false
    };
    p.Start();
    reportNumber = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

这是我遇到的错误

System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'

正在编辑

看这里,我发现我可以 运行 通过 cmd

作为

var proc = Process.Start(@"cmd.exe ",@"/c C:\Users\hed-b\Desktop\PulserTester.appref-ms")

但是我该如何使用 RedirectStandardOutput 呢?

看起来您打算启动 CMD 和 运行 一个命令,但您的代码只是试图 运行 该命令作为一个应用程序。

尝试这样的事情。

    var p = new Process();
    p.StartInfo = new ProcessStartInfo(@"cmd.exe")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
        Arguments = "/c C:\Users\hed-b\Desktop\PulserTester.appref-ms"
    };
    p.Start();
    reportNumber = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

使用阻塞 ReadToEnd 将在该代码 运行 时暂停您的线程,并使捕获错误输出也变得更加困难 - 查看此答案以了解非阻塞解决方案的演示捕获标准数据和标准错误:ProcessInfo and RedirectStandardOutput