简体   繁体   English

如何从命令行将文件传递到程序

[英]How to pass file to the program from the command line

I am writing a wpf application which should draw lines due points given in a file. 我正在编写一个wpf应用程序,该程序应绘制文件中给定的点。

How can I pass a file in the command line to my c# program? 如何在命令行中将文件传递给C#程序? For example something like 例如类似

MyProgram.exe < file.txt

In addition how can I do this in visual studio for debugging? 另外,如何在Visual Studio中进行调试? I know I can set command line args and can read them with 我知道我可以设置命令行参数,并且可以使用

var args = System.Environment.GetCommandLineArgs().ToList();

You have to do is call the program in this way, "filedetails.exe myfile.txt" 您要做的就是以这种方式调用程序“ filedetails.exe myfile.txt”

Example : 范例:

C:\Users\FILEREADER>filedetails.exe myfile.txt

How can I pass a file in the command line to my c# program? 如何在命令行中将文件传递给C#程序? For example > something like 例如>类似

MyProgram.exe <file.txt

You can try this Console.OpenStandardInput() 您可以尝试使用此Console.OpenStandardInput()

I don't think it is possible to pass file content as a parameter. 我认为不可能将文件内容作为参数传递。 You currently pass file content correctly: 您当前正确地传递了文件内容:

MyProgram.exe < file.txt

all you need is to read it, I put a small cmd application: 您所需要做的就是阅读它,我放了一个小的cmd应用程序:

    static void Main()
    {
        string line;
        while ((line = Console.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }

In wpf application: 在WPF应用程序中:

    public MainWindow()
    {
        InitializeComponent();
        var result = "";
        string line;
        while ((line = Console.ReadLine()) != null)
        {
            result += (line);
        }
        MessageBox.Show(result);
    }

Check here for more information about about command line redirections. 在此处查看有关命令行重定向的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM