Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从命令行参数中的文件中读取其他标准参数?(模拟Python的文件输入)_C#_.net - Fatal编程技术网

C# 如何从命令行参数中的文件中读取其他标准参数?(模拟Python的文件输入)

C# 如何从命令行参数中的文件中读取其他标准参数?(模拟Python的文件输入),c#,.net,C#,.net,我希望我的应用程序从命令行参数指定的文件或中的标准文件中读取,以便用户可以使用它myprogram.exe data.txt或otherprogram.exe | myprogram.exe。我怎样才能在C#中做到这一点 我会用Python编写 import fileinput for line in fileinput.input(): process(line) 这将迭代sys.argv[1:]中列出的所有文件的行,如果列表为空,则默认为sys.stdin。如果文件名为“-”,它

我希望我的应用程序从命令行参数指定的文件或中的标准文件中读取,以便用户可以使用它
myprogram.exe data.txt
otherprogram.exe | myprogram.exe
。我怎样才能在C#中做到这一点


我会用Python编写

import fileinput
for line in fileinput.input():
    process(line)
这将迭代sys.argv[1:]中列出的所有文件的行,如果列表为空,则默认为sys.stdin。如果文件名为“-”,它也将替换为sys.stdin


Perl的
和Ruby的
ARGF
也同样有用

实际上,这非常容易

在C代码编辑器中,您可以执行以下操作:

public static void Main(string[] args) {
    //And then you open up a file. 
    using(Streamreader sr = new Streamreader(args[0])) {
            String line = sr.ReadToEnd();
            Console.WriteLine(line);
    }
}
另一个好主意是迭代c#集合中的items args,以便可以将多个文件作为输入。例如:
main.exe file1.txt file2.txt file3.txt
等等

您可以使用特殊的for循环修改上述代码,如下所示:

foreach(string s in args) {
    using( Streamreader sr = new Streamreader(s) ) {
        String line = sr.ReadToEnd();
        Console.WriteLine(line);
    }
}
祝你好运

使用

 static void Main(string[] args)
然后在for循环中使用args.length迭代每个输入


使用示例:

stdin
通过
控制台以
文本阅读器的身份向您公开。只需为您的输入声明一个
TextReader
变量,该变量使用
控制台。在
或您选择的文件中,并将其用于所有输入操作

static TextReader input = Console.In;
static void Main(string[] args)
{
    if (args.Any())
    {
        var path = args[0];
        if (File.Exists(path))
        {
            input = File.OpenText(path);
        }
    }

    // use `input` for all input operations
    for (string line; (line = input.ReadLine()) != null; )
    {
        Console.WriteLine(line);
    }
}
否则,如果重构以使用此新变量的成本太高,则始终可以使用
Console.SetIn()
Console.In
重定向到您的文件

试试这个:

public void Main(string[] args)
        {
            if (args.Count() > 0)
            {

                byte[] byteArray = Encoding.UTF8.GetBytes(args[0]);
                MemoryStream stream = new MemoryStream(byteArray);

                StreamReader sr = new StreamReader(stream);
                String line = sr.ReadToEnd();
                Console.WriteLine(line);
            }
            Console.ReadLine();
        }

args[0]是一个字符串,必须先将其转换为流,然后才能传递给StreamReader构造函数

静态void Main(string[]args),并读取args数组?这里还有一些建议:相关文章-&这正是我想要的。谢谢添加一些关于此答案如何帮助OP解决当前问题的解释
public void Main(string[] args)
        {
            if (args.Count() > 0)
            {

                byte[] byteArray = Encoding.UTF8.GetBytes(args[0]);
                MemoryStream stream = new MemoryStream(byteArray);

                StreamReader sr = new StreamReader(stream);
                String line = sr.ReadToEnd();
                Console.WriteLine(line);
            }
            Console.ReadLine();
        }