简体   繁体   English

如何重定向输出作为命令行输入?

[英]How to redirect output as input in command line?

I want to write a console application which can handle input in command line as below: 我想编写一个控制台应用程序,该应用程序可以处理以下命令行中的输入:

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

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

In my TestPipe code, I just receive input1 as print input1 as below: 在我的TestPipe代码中,我只收到input1作为打印input1,如下所示:

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

Now I want to run TestPipe in another ConsoleApplication.exe which has the code as below: 现在,我要在另一个具有以下代码的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();

However, I got unhandled exception at } while (inputTxt.Length != 0); 但是,我在} while (inputTxt.Length != 0);遇到未处理的异常} while (inputTxt.Length != 0);

Question: 题:

1) How to change the code so I could run type file1.csv file2.csv | TestPipe.exe input1 1)如何更改代码,以便我可以运行type file1.csv file2.csv | TestPipe.exe input1 type file1.csv file2.csv | TestPipe.exe input1 in command line? 命令行中的type file1.csv file2.csv | TestPipe.exe input1

2) How to get the file1.csv, file2.csv string value in TestPipe? 2)如何在TestPipe中获取file1.csv,file2.csv字符串值?

3) I want to run nested redirect input like this: ((type file1.csv file2.csv | TestPipe +) & (type file3.csv file4.csv | TestPipe +)) | TestPipe + 3)我想运行嵌套的重定向输入,例如: ((type file1.csv file2.csv | TestPipe +) & (type file3.csv file4.csv | TestPipe +)) | TestPipe + ((type file1.csv file2.csv | TestPipe +) & (type file3.csv file4.csv | TestPipe +)) | TestPipe +

Start Your application with following command 使用以下命令启动您的应用程序

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

and you can access those values in TestPipe.exe using args[] in Main 您可以使用Main中的args []访问TestPipe.exe中的那些值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM