【问题标题】:Run a C# exe with parameters that is to start another application and get output from console运行带有参数的 C# exe,以启动另一个应用程序并从控制台获取输出
【发布时间】:2018-09-18 08:51:15
【问题描述】:

我有一个 exe,它有一些参数 - 另一个应用程序的路径和一些要从该应用程序打开的文件。将有一个输出作为该应用程序的一部分,该输出将显示在我的 exe 控制台中。 但我无法从控制台获取输出。 我有代码:

ProcessStartInfo psi = new ProcessStartInfo("\"" + dllpath + "\\newapplication.exe" + "\"");
Process p = new Process();
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
p.Start();

该过程成功启动,然后我必须在通过另一个类发生的过程中打开一个文件。所以文件打开后,会进行一些提取,结果会显示在控制台上。 当我给p.WaitForExit(); 时,除了启动应用程序之外什么都没有发生!我如何根据我的代码检索 StandardOutput 上的输出?需要帮助!

【问题讨论】:

    标签: c# console output


    【解决方案1】:

    这是正确的做法:

    string outputProcess = "";
    string errorProcess = "";
    
    using (Process process = new Process())
    {
        process.StartInfo.FileName = path;
        process.StartInfo.Arguments = arguments;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.Start();
        outputProcess = process.StandardOutput.ReadToEnd();
        errorProcess = process.StandardError.ReadToEnd();
        process.WaitForExit();
    }
    

    当你有一个IDisposable 对象时,记得使用using 语句

    【讨论】:

    • 它开始这个过程。但是我希望该过程对文件进行一些提取..这根本没有发生。该过程只是保持打开状态,什么都不做。
    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多