【问题标题】:How to pass file to the program from the command line如何从命令行将文件传递给程序
【发布时间】:2015-08-13 04:07:49
【问题描述】:

我正在编写一个 wpf 应用程序,它应该根据文件中给出的点画线。

如何将命令行中的文件传递给我的 c# 程序? 例如像

MyProgram.exe < file.txt

此外,我如何在 Visual Studio 中执行此操作以进行调试? 我知道我可以设置命令行参数并可以使用

读取它们
var args = System.Environment.GetCommandLineArgs().ToList();

【问题讨论】:

    标签: c#


    【解决方案1】:

    你要做的就是用这种方式调用程序,“filedetails.exe myfile.txt”

    例子:

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

    【讨论】:

      【解决方案2】:

      如何将命令行中的文件传递给我的 c# 程序?例如 > 类似

      MyProgram.exe &lt;file.txt

      你可以试试这个Console.OpenStandardInput()

      【讨论】:

        【解决方案3】:

        我认为不可能将文件内容作为参数传递。您当前正确传递文件内容:

        MyProgram.exe < file.txt
        

        你只需要阅读它,我放了一个小 cmd 应用程序:

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

        在 wpf 应用程序中:

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

        Check here 了解有关命令行重定向的更多信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-06
          • 2016-06-20
          • 2010-10-04
          • 2015-06-08
          • 2013-07-12
          • 2011-05-20
          相关资源
          最近更新 更多