【问题标题】:How to redirect output as input in command line?如何在命令行中将输出重定向为输入?
【发布时间】:2018-03-06 15:51:07
【问题描述】:

我想编写一个控制台应用程序,它可以处理命令行中的输入,如下所示:

type file1.csv file2.csv | TestPipe.exe input1

((type file1.csv file2.csv | TestPipe +) & (type file3.csv file4.csv | TestPipe +)) | TestPipe +

在我的 TestPipe 代码中,我只收到 input1 作为打印 input1,如下所示:

static void Main(string[] args)
    {
        Console.WriteLine(string.Format("Your input is {0}", args[0]));
    }

现在我想在另一个 ConsoleApplication.exe 中运行 TestPipe,代码如下:

        Process Test = new Process();
        Test.StartInfo.FileName = @"C:\Users\Administrator\Downloads\TestPipe\TestPipe\bin\Debug\TestPipe.exe";
        Test.StartInfo.UseShellExecute = false;
        Test.StartInfo.RedirectStandardInput = true;
        Test.Start();
        StreamWriter myStreamWriter = Test.StandardInput;
        string inputTxt;
        do
        {
            inputTxt = Console.ReadLine();
        } while (inputTxt.Length != 0);

        myStreamWriter.Close();
        Test.WaitForExit();
        Test.Close();

但是,我在} while (inputTxt.Length != 0); 遇到了未处理的异常

问题:

1) 如何更改代码以便我可以在命令行中运行type file1.csv file2.csv | TestPipe.exe input1

2)TestPipe中如何获取file1.csv、file2.csv字符串值?

3) 我想像这样运行嵌套重定向输入:((type file1.csv file2.csv | TestPipe +) & (type file3.csv file4.csv | TestPipe +)) | TestPipe +

【问题讨论】:

    标签: c# redirect command-line


    【解决方案1】:

    使用以下命令启动您的应用程序

    var proc = System.Diagnostics.Process.Start("D://TestPipe.exe", "type file1.csv file2.csv | TestPipe.exe input1");
    

    您可以使用 Main 中的 args[] 在 TestPipe.exe 中访问这些值

        static void Main(string[] args)
        {
           //args[0] == type 
           //args[1] == file1.csv
           //args[1] == file2.csv
        }
    

    【讨论】:

    • TestPipe.exe中如何获取file1.csv、file2.csv?
    • 希望编辑后的答案对您有所帮助!
    • 带有字符串[] args 的 Main 子例程是否位于 TestPipe.exe 或另一个 ConsoleApplication.exe 中?
    • 在TestPipe.exe中
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多