Reputation: 144
I would like to execute a shell line using---> Process myProcess = new Process();
the line is something like:
pathMyProgram -options < file.txt
I have done
Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName=pathMyProgram;
pProcess.StartInfo.Arguments=-optins < file.txt
... but it doesn`t work ( due to the redirection...)
So after reading I have tried
enter code here
Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName=pathMyProgram;
pProcess.StartInfo.Arguments=-optins
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
pProcess.Start();
pProcess.StandardInput.WriteLine(file.txt);
and it continue without working, any help?
Upvotes: 0
Views: 386
Reputation: 55457
Don't confuse shell redirection and the cmd.exe
program. They look and act similar but are still different things. Arguments such as <
and >
are specific to cmd.exe
If you pass these into your process they will be passed literally and you will need to parse them manually.
See the answer here, specifically EDIT 2 and 3. Also checkout Raymond Chen's blog post on it here
Upvotes: 0
Reputation: 203812
You should replace :
pProcess.StandardInput.WriteLine(file.txt);
with something like:
try
{
pProcess.StandardInput.Write(File.ReadAllText("file.txt"));
}
finally
{
pProcess.StandardInput.Close();
}
That's assuming you can read the entire file into memory easily. If it's a large file you'll want to read it a line at a time and pass it to the
Upvotes: 1
Reputation: 27974
Did you consider using "strings"
?
pProcess.StartInfo.FileName = "pathMyProgram";
pProcess.StartInfo.Arguments = "-optins < file.txt";
Also note you will need to escape backslashes, like this:
"C:\\Path\\To\\Program.exe"
You should better read some beginners guide to C# to grasp the basics. (Unless I missed something in the meaning of your question.)
Upvotes: 0