How to pass filename to StandardInput (Process) in C#?

Posted by Cosmo on Stack Overflow See other posts from Stack Overflow or by Cosmo
Published on 2010-06-02T09:07:18Z Indexed on 2010/06/02 9:13 UTC
Read the original article Hit count: 375

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET