質問

パラメータを使用してコンソールアプリケーションをプログラムする方法を知っています。例:myProgram.exe param1 param2

私の質問は、どうすればプログラムを|で動作させることができますか、例:echo <!> quot; word <!> quot; | 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);
    }
}

これにより、2つの使用方法が可能になります。 標準入力から読み取る:

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

またはパイプ入力から読み取る

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

他のソリューションとpeek()を組み合わせた別の代替ソリューションを次に示します。

Peek()なしで<!> quot; type t.txt | prog.exe <!> quot;ここで、t.txtは複数行のファイルです。しかし、ただ<!> quot; prog.exe <!> quot;または<!> quot; echo hi | prog.exe <!> quot;正常に動作しました。

このコードは、パイプ入力のみを処理するためのものです。

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 <!> lt; input.txt

Stringdinを使用して、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