首页 > 解决方案 > C# 使用 Process.Start() 作为 cmd 和活动 RedirectStandardOutput 的参数执行应用程序

问题描述

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

我必须使用该选项"Prefer 32-bit",因为我正在使用带有连接字符串的 Access DBProvider=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:“指定的可执行文件不是此 OS 平台的有效应用程序。”

编辑

通过看这里,我看到我可以通过 cmd 运行它

.Net Core 2.0 Process.Start 抛出“指定的可执行文件不是此 OS 平台的有效应用程序”

作为

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

但是我怎样才能以这种方式使用 RedirectStandardOutput 呢?

标签: c#clickonce

解决方案


看起来您打算启动 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 和 RedirectStandardOutput


推荐阅读