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

≯℡__Kan透↙ 提交于 2021-01-29 14:26:25

问题


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

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.

This is my execution code :

    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();

This is the error I have got

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

Editing

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

.Net Core 2.0 Process.Start throws "The specified executable is not a valid application for this OS platform"

as

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

But How can I use RedirectStandardOutput this way?


回答1:


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

Try something like this.

    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();

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



来源:https://stackoverflow.com/questions/51153242/c-sharp-execute-application-using-process-start-as-params-to-cmd-and-active-re

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!