简体   繁体   中英

Passing output of type command as a command line argument in C#

I want to open a JSON file using the Win32 command type and re-direct its output to my C# program through command line arguments.

  • type Demo.json

gives me

{
   "Message" : "Hello World"
}

I want to pass it to my C# program via command line arguments as follows

  • C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\csc.exe Demo.cs
  • Demo type Demo.json

But I get output as: Demo.json

Demo.cs includes:

using System;
namespace Demo
{
  public class Program
  {
      public static void Main(string[] args)
      {
             Console.WriteLine(args[0]);
      }
  } 
}

I want to redirect the output of the type command to pass it to the command line argument.

Don't pass the contents of a file through the command line arguments. Instead, pipe the data into stdin then have your program read from stdin:

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

        Console.WriteLine("Read from stdin: " + contents);
    }
}

And to run it:

C:\Projects\ConsoleApp1\bin>type my-file.txt | ConsoleApp1.exe

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