【问题标题】:How to mimic Stdin input when running an exe from C# using create process?使用创建进程从 C# 运行 exe 时如何模仿标准输入?
【发布时间】:2021-07-08 22:56:50
【问题描述】:

我有一个音频转换器 .exe,我想将它封装在 C# 程序中,用于 UI 和输入等。 要使用 AudioConverter.exe,它从控制台运行,后缀为“ ouputFile”。 所以整行的内容类似于

C:\\User\Audioconverter.exe < song.wav > song.ogg

到目前为止,我已经能够在 C# 之外成功启动转换器,我已经设法让转换器在挂起状态下通过 C# 中的创建进程运行(没有输入和输出文件)。 到目前为止,我在 C# 中的代码与本网站上给出的答案非常相似:

using System;
using System.Diagnostics;

namespace ConverterWrapper2
{
    class Program
    {
        static void Main()
        {
            LaunchCommandLineApp();
        }
        static void LaunchCommandLineApp()
        {
            // For the example
            const string ex1 = "C:\\Users\\AudioConverter.exe";
            const string ex2 = "C:\\Users\\res\\song.wav";
            const string ex3 = "C:\\Users\\out\\song.ogg";

            // Use ProcessStartInfo class
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = false;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "AudioConverter2.exe";
            startInfo.WindowStyle = ProcessWindowStyle.Normal;
            startInfo.Arguments = ex1 + " < " + ex2 + " > " + ex3; \\Process is ran successfully without the addition of input and output files, but hangs waiting for files.

            try
            {
                using (Process exeProcess = Process.Start(startInfo))
                {
                    exeProcess.WaitForExit();
                }
            }
            catch
            {
                // Log error.
            }
        }
    }
}

到目前为止,转换器 exe 还不能正确启动,这让我问这个问题是标准输入的输入与参数不同吗?

不管我需要模仿这种输入方式,并且会感谢任何信息。我曾以为我可以将输入和输出文件作为参数传递,但我运气不佳。

【问题讨论】:

    标签: c# stdin oggvorbis


    【解决方案1】:
    startInfo.Arguments = ex1 + " < " + ex2 + " > " + ex3; \\Process is ran successfully without the addition of input and output files, but hangs waiting for files.
    

    那行不通。

    A.exe &lt; B &gt; C不是 进程 A.exe 使用参数 &lt; B &gt; C 调用的。这更像是一个 shell 指令:

    • 开始A.exe不带参数,
    • 读取文件B并将其内容重定向到新进程的标准输入和
    • 将新进程的标准输出写入文件C

    在 C# 中有两种选择:

    1. 您可以使用 shell 的帮助,即,您可以使用参数 /c C:\User\Audioconverter.exe &lt; song.wav &gt; song.ogg 或启动 cmd.exe

    2. 您可以在 C# 中重新实现 shell 正在执行的操作。可以在这个相关问题中找到一个代码示例:

    【讨论】:

    • 好吧,选项一有效,但我不是 100% 认为在第二种方法中使用它是否明智。第二个将需要更多测试,看看我是否可以暗示它。不过谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多