C#使用Process.Start()作为cmd和活动RedirectStandardOutput的参数来执行应用程序 [英] C# Execute Application Using Process.Start() as params to cmd and active RedirectStandardOutput

查看:124
本文介绍了C#使用Process.Start()作为cmd和活动RedirectStandardOutput的参数来执行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Visual Studio Clickones and try to execute (.appref-ms) application and using RedirectStandardOutput...

我必须使用选项首选32位" ,因为我正在使用Access DB,其连接字符串为 Provider = Microsoft.Jet.OLEDB.4.0.

I have to use the option "Prefer 32-bit" because I am using Access DB with connection string as 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:'指定的可执行文件不是此OS平台的有效应用程序.'

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

编辑

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

By look here, I see that I can run it by cmd

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

但是如何以这种方式使用RedirectStandardOutput?

But How can I use RedirectStandardOutput this way?

推荐答案

看起来您打算启动CMD并运行命令,但是您的代码只是尝试将该命令作为应用程序运行.

Looks like you're intending to start CMD and run a command, but your code just tries to run that command as an application.

尝试这样的事情.

    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

Using the blocking ReadToEnd will pause your thread while that code runs and makes it harder to capture error output also - Have a look at this answer for a demonstration of a non blocking solution that captures both standard data and standard err : ProcessInfo and RedirectStandardOutput

这篇关于C#使用Process.Start()作为cmd和活动RedirectStandardOutput的参数来执行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆