开发者

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

I'm creating my first command application in Visual Studio.

I was wondering what is the command to read lines from a batch file. I need to be able to read the first line of a batch file then call some method with the parameters I got from the first line, after that I need to be able to read the second line from a batch file 开发者_JAVA技巧and call the same method, and so forth until the end of the file.

I already know how to call methods. I just need to know how to read the batch file.


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


A batch file is a text file, so you can do:

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

Or if you want to read lazily (available in .net 4):

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

But since batch files are typically rather small, I'd most likely use ReadAllLines.


If you want the command line arguments passed to your application you get them using Environment.GetCommandLineArgs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜