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

我的问题是,如何才能让我的计划与|,例如:回声“字” | myProgram.exe?

有帮助吗?

解决方案

您需要使用Console.Read()Console.ReadLine()就好像你在读用户输入。管道代替用户输入透明。你不能同时使用容易(虽然我敢肯定,这是完全可能的......)。

修改

一个简单cat样式的程序:

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

    }
}

和运行时,正如所料,输出:

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

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

其他提示

下面将不暂停输入应用程序和数据时的工作原理的不输送。一个黑客攻击的位;并且由于错误捕获,表现在众多管道调用由可能缺乏,但...容易。

public static void Main(String[] args)
{

    String pipedText = "";
    bool isKeyAvailable;

    try
    {
        isKeyAvailable = System.Console.KeyAvailable;
    }
    catch (InvalidOperationException expected)
    {
        pipedText = System.Console.In.ReadToEnd();
    }

    //do something with pipedText or the args
}

在.NET 4.5它的

if (Console.IsInputRedirected)
{
    using(stream s = Console.OpenStandardInput())
    {
        ...

这是做到这一点的方式:

static void Main(string[] args)
{
    Console.SetIn(new StreamReader(Console.OpenStandardInput(8192))); // This will allow input >256 chars
    while (Console.In.Peek() != -1)
    {
        string input = Console.In.ReadLine();
        Console.WriteLine("Data read was " + input);
    }
}

这允许两个使用方法。从读取的标准输入

C:\test>myProgram.exe
hello
Data read was hello

或读出的管道输入

C:\test>echo hello | myProgram.exe
Data read was hello

下面是从其他的解决方案加偷看()。

放在一起另一替代解决方案

如果没有PEEK()我正在经历该应用不会没有CTRL-C在端做时返回“类型t.txt | prog.exe”,其中t.txt是一个多行文件。但只是 “prog.exe” 或 “回声喜| prog.exe” 工作得很好。

此代码旨在仅过程管道输入。

static int Main(string[] args)
{
    // if nothing is being piped in, then exit
    if (!IsPipedInput())
        return 0;

    while (Console.In.Peek() != -1)
    {
        string input = Console.In.ReadLine();
        Console.WriteLine(input);
    }

    return 0;
}

private static bool IsPipedInput()
{
    try
    {
        bool isKey = Console.KeyAvailable;
        return false;
    }
    catch
    {
        return true;
    }
}

Console.In是围绕标准输入流包裹的TextReader的参考。当管道大量的数据到你的程序,它可能是更容易与工作方式。

有与供给的例子中的问题。

  while ((s = Console.ReadLine()) != null)

将卡住等待输入如果程序不经管道数据发射。因此,用户必须手动按下任何键退出程序。

这也将为工作

  

C:\ MyApp.exe的

我不得不使用StringBuilder操纵从STDIN时捕获的输入:

public static void Main()
{
    List<string> salesLines = new List<string>();
    Console.InputEncoding = Encoding.UTF8;
    using (StreamReader reader = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding))
    {
        string stdin;
        do
        {
            StringBuilder stdinBuilder = new StringBuilder();
            stdin = reader.ReadLine();
            stdinBuilder.Append(stdin);
            var lineIn = stdin;
            if (stdinBuilder.ToString().Trim() != "")
            {
                salesLines.Add(stdinBuilder.ToString().Trim());
            }

        } while (stdin != null);

    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top