简体   繁体   中英

c# console: how can i read piped input from the command line

I'm a bit confused. I've been looking through previous answers to the above question and NONE of them actually work for me, so I'm not sure what i'm doing wrong.

I have a simple c# console app: (outputsomething.exe)

static void Main(string[] args) {
    Console.Out.Write("This is a test");
}

And another one that takes some args and piped input from the above code, like this: dostuffwithinput.exe some args |outputsomething.exe

First problem is that I can't work out how to "read" the piped input, as I said above none of the existing answers work for me, reading from stdin (Console.In) just doesn't capture anything.

Secondly, after piping something in I can no longer write anything out to stdout (Console.Out).

The code I have is very simple it just writes out the args to a file, together with a few console attributes and tries to read from Console.In - which it does but it only reads input that I type in.

But with piped input it just never manages to capture what is piped in and the last output is never sent to the console.

    static void Main(string[] args) {
        bool ior = Console.IsInputRedirected;
        bool keyavail = Console.KeyAvailable;

        char[] chrs = new char[4];
        int chr = Console.In.ReadBlock(chrs, 0, 4);

        using (StreamWriter stream = new StreamWriter("fred.txt")) {
            foreach (var arg in args) {
                stream.WriteLine(arg);
                Console.WriteLine(arg);
            }
            stream.WriteLine("input = " + new string(chrs));
            stream.WriteLine("io redirectirected = " + ior);
            stream.WriteLine("is key available = " + keyavail);
        }
        Console.WriteLine("fin");
        Console.Out.Flush();
    }

(I'm also having serious problems with this pile-of-crap editor that keeps showing a red warning about indentation show i've have to remove a lot of examples to get it in a sendable form!)

What am I doing wrong? (this is on windows 10/9879)

TIA

Tried the same with the below example codes. It is working for me.

WriteText

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Test Line1");
            Console.WriteLine("test Line2");
        }
    }

ProcessInput

class Program
    {
        static void Main(string[] args)
        {
            string s=Console.In.ReadToEnd();

            Console.WriteLine("Redirected Text: " + s);

        }
    }

I invoked the app using WriteText.exe | ProcessInput.exe It showed the output string exactly passed by the WriteText.exe

Please let me know, if this helps.

如果要从outputsomething.exe管道outputsomething.exedostuffwithinput.exe ,则应为

outputsomething.exe|dostuffwithinput.exe some args

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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