繁体   English   中英

如何在Visual Studio中对命令应用程序进行编程以读取批处理文件

[英]How to program a command application in visual studio to read a batch file

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

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

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

using(StreamReader batchReader = new StreamReader("path to batch file"))
{
    string batchCommand;
    while(!batchReader.EndOfStream)
    {
        batchCommand = batchReader.ReadLine();
        // do your processing with batch command
    }
}

批处理文件是文本文件,因此您可以执行以下操作:

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

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

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

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


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

暂无
暂无

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

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