user687554
user687554

Reputation: 11131

Pipe Data From Command Line into C# Console App

I have a basic C# Console app that looks like this:

static void Main(string[] args)
{
  if (args.Length > 0)
  {
    foreach (var arg in args)
    {
      Console.WriteLine("arg: " + arg);
    }
  }
  else
  {
    Console.WriteLine("No args received from standard output.");
  }
}

My app will eventually receive data from a third-party app that will pipe data to this console app. In an effort to test this scenario, I tried running the following:

echo "hello world" | MyConsoleApp.exe

When I ran it, I saw No args received from standard output.. However, I was expecting to see hello world. What am I doing wrong?

Upvotes: 0

Views: 680

Answers (1)

ubi
ubi

Reputation: 4399

Piped outputs from another command are not passed to your application as command line arguments (i.e. args[] in Main()). Rather, they need to be read in by your application from the standard input (stdin). You need to use Console.Read() or Console.ReadLine() for that.

Upvotes: 1

Related Questions