[英]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. 我想使用Win32命令
type
打开JSON文件,然后通过命令行参数将其输出重定向到C#程序。
type Demo.json
gives me 给我
{
"Message" : "Hello World"
}
I want to pass it to my C# program via command line arguments as follows 我想通过命令行参数将其传递给我的C#程序,如下所示
C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\csc.exe Demo.cs
Demo type Demo.json
But I get output as: Demo.json
但我得到的输出为:
Demo.json
Demo.cs includes: Demo.cs包括:
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. 我想重定向
type
命令的输出,以将其传递给命令行参数。
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:
相反,将数据通过管道传输到stdin中,然后从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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.