如何在命令行中将输出重定向为输入?

时间:2018-03-06 15:51:07

标签: c# redirect command-line

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

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

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

在我的TestPipe代码中,我只接收input1作为print 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 +

1 个答案:

答案 0 :(得分:0)

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

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
    }
相关问题