【问题标题】:How to program a command application in visual studio to read a batch file如何在 Visual Studio 中编写命令应用程序以读取批处理文件
【发布时间】:2011-03-23 18:23:09
【问题描述】:

我正在 Visual Studio 中创建我的第一个命令应用程序。

我想知道从批处理文件中读取行的命令是什么。我需要能够读取批处理文件的第一行,然后使用从第一行获得的参数调用一些方法,之后我需要能够从批处理文件中读取第二行并调用相同的方法,以此类推,直到文件结束。

我已经知道如何调用方法。我只需要知道如何读取批处理文件。

【问题讨论】:

  • 您真的要读取批处理文件吗?还是您希望将参数从调用它的批处理文件传递到您的程序中?

标签: c# batch-file command


【解决方案1】:
using(StreamReader batchReader = new StreamReader("path to batch file"))
{
    string batchCommand;
    while(!batchReader.EndOfStream)
    {
        batchCommand = batchReader.ReadLine();
        // do your processing with batch command
    }
}

【讨论】:

    【解决方案2】:

    批处理文件是一个文本文件,所以你可以这样做:

    string[] lines = File.ReadAllLines(filename);
    

    或者,如果您想懒惰地阅读(在 .net 4 中可用):

    IEnumerable<string> lines=File.ReadLines(filename);
    

    但由于批处理文件通常很小,我很可能使用ReadAllLines


    如果您想将命令行参数传递给您的应用程序,您可以使用 Environment.GetCommandLineArgs 获取它们

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 2011-10-20
      • 2023-01-14
      • 2011-08-18
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      相关资源
      最近更新 更多