C#控制台收到管道输入 [英] C# Console receive input with pipe

查看:126
本文介绍了C#控制台收到管道输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何编程与参数的控制台应用程序,例如:myProgram.exe参数1参数2

I know how to program Console application with parameters, example : myProgram.exe param1 param2.

我的问题是,如何才能让我的程序使用|,例如:回声字| myProgram.exe?

My question is, how can I make my program works with |, example : echo "word" | myProgram.exe?

推荐答案

您需要使用 Console.Read()到Console.ReadLine( ),如果你在读用户输入。管代替用户输入透明。你不能同时使用容易(虽然我敢肯定,它很可能...)。

You need to use Console.Read() and Console.ReadLine() as if you were reading user input. Pipes replace user input transparently. You can't use both easily (although I'm sure it's quite possible...).

编辑:

一个简单的样式的程序:

A simple cat style program:

class Program
{
    static void Main(string[] args)
    {
        string s;
        while ((s = Console.ReadLine()) != null)
        {
            Console.WriteLine(s);
        }

    }
}

和运行时,如预期的输出:

And when run, as expected, the output:


C:\...\ConsoleApplication1\bin\Debug>echo "Foo bar baz" | ConsoleApplication1.exe
"Foo bar baz"

C:\...\ConsoleApplication1\bin\Debug>

这篇关于C#控制台收到管道输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆