Redirecting standard input

Lets say you have to start a program with a parameters from a file. With a command line you will write simply something like that:
program.exe < ParamFile.txt
Using System.Diagnostics.Process you cannot redirect a content of a file to an external application like that. You have to redirect standard input to you process. You can do it like this:
StreamWriter paramFileStreamWriter = someProcess.StandardInput;
string[] paramData = File.ReadAllLines(paramFile);
for (int i = 0; i < procedData.Length; i++)
{
paramFileStreamWriter.WriteLine(paramData[i]);
}
paramFileStreamWriter.Close();

Some more information is to by found on MSDN.
Originally published at Saturday, November 11, 2006

Leave a Reply

Your email address will not be published. Required fields are marked *