0

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?

Reno
  • 33,594
  • 11
  • 89
  • 102
jobormo
  • 144
  • 2
  • 9

3 Answers3

1

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

Servy
  • 202,030
  • 26
  • 332
  • 449
0

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

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
0

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

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • It looks like you didn't read the second half of the OP. He realized this himself, but he's just been copy/pasting code from various sources without understanding what it does and clearly this is causing him problems. – Servy Jan 25 '12 at 16:51
  • I read it but although I think the OP realized the potential problem I don't think he actually understands it. His edit still looks like he's trying to perform the redirection in a different way. The posts I linked to show how to pass these to `cmd.exe` with the `/C` switch which should do what he's trying to do. – Chris Haas Jan 25 '12 at 16:57
  • I did the first one without any redirection, just commands and I have no problems, the problems came when I tried the redirection. and i wrote what I found..and like it doesn`t work I asked there..now I am reading the Raymond Chen's blog post, the only one who trie to help and not to critize – jobormo Jan 25 '12 at 17:01
  • @jobormo, I don't think anyone was trying to criticize you directly. On this site we get many, many questions from people with skills all over the place. Some are crazy awesome ninja programmers and some are just beginners. Unfortunately there isn't an easy way for us to determine that with newer users. So when we see code that won't even compile the first thing we do is try to correct that problem. Believe it or not, there a many posts every day that are solved just by fixing the compiling errors. Missing quotes, slashes, etc. – Chris Haas Jan 25 '12 at 17:20