1

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 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.

1
  • 2
    Do you really want to read a batch file? Or do you want the parameters passed into your program from the batch file calling it? Commented Mar 23, 2011 at 18:26

2 Answers 2

2
using(StreamReader batchReader = new StreamReader("path to batch file")) { string batchCommand; while(!batchReader.EndOfStream) { batchCommand = batchReader.ReadLine(); // do your processing with batch command } } 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.