如何使用一个txt文件作为命令行参数?(How to use a txt file as comma

2019-08-01 05:55发布

我有这样一个.txt文件:
6 4
1 2
2 3
3 4
4 5
1 2 4 5

我怎样才能用这个作为在C#中的命令行参数?

Answer 1:

如果你打算给你的程序数据program.exe < data.txt ,这就是所谓的从标准输入读取 。 您可以通过.NET我们做到这一点Console.OpenStandardInput与

new StreamReader(Console.OpenStandardInput())

或者,如果你宁愿你的程序运行program.exe data.txt ,开始

void Main(string[] args)
{
    File.ReadLines(args[0])
}


Answer 2:

您可以接受在命令行(与路径一起)的文件名,并在你的应用程序中打开该文件,一行阅读线和处理的所有行。



Answer 3:

转到Solution Explorer > Right click on project file > Select Properties from context menu 。 这将打开项目的属性窗口。 现在去Debug tab >转到Start options section在右窗格中。 把里面的文件全路径Command line arguments文本框中如下图所示:

< "D:\Rasik\input01.txt"

然后,你可以写代码像往常一样,你会开始从文件中获取输入:

class Program
{        
    static void Main(string[] args)
    {
        var textInFirstLineOfFile = Console.ReadLine();
    }
}


文章来源: How to use a txt file as command line argument?