How to pass filename to StandardInput (Process) in C#?
- by Cosmo
Hello Guys!
I'm using the native windows application spamc.exe (SpamAssassin - sawin32) from command line as follows:
C:\SpamAssassin\spamc.exe -R < C:\email.eml
Now I'd like to call this process from C#:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"C:\SpamAssassin\spamc.exe";
p.StartInfo.Arguments = @"-R";
p.Start();
p.StandardInput.Write(@"C:\email.eml");
p.StandardInput.Close();
Console.Write(p.StandardOutput.ReadToEnd());
p.WaitForExit();
p.Close();
The above code just passes the filename as string to spamc.exe (not the content of the file). However, this one works:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"C:\SpamAssassin\spamc.exe";
p.StartInfo.Arguments = @"-R";
p.Start();
StreamReader sr = new StreamReader(@"C:\email.eml");
string msg = sr.ReadToEnd();
sr.Close();
p.StandardInput.Write(msg);
p.StandardInput.Close();
Console.Write(p.StandardOutput.ReadToEnd());
p.WaitForExit();
p.Close();
Could someone point me out why it's working if I read the file and pass the content to spamc, but doesn't work if I just pass the filename as I'd do in windows command line?