我使用的本機窗口應用spamc.exe(SpamAssassin的 - sawin32)從命令行如下:如何在C#中傳遞文件名到StandardInput(Process)?
C:\ SpamAssassin的\ spamc.exe -R < C:\ email.eml
現在我想從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();
上面的代碼只傳遞文件名作爲字符串spamc.exe(不是文件的內容)。然而,這一個工程:
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();
有人能指出我爲什麼它的工作,如果我讀的文件,並傳遞給spamc的內容,但如果我只是通過文件名,因爲我想在做不起作用Windows命令行?
好的,那麼傳遞文件的正確方法是什麼?除了第二個例子還有其他解決方案嗎? – Cosmo 2010-06-02 09:18:05
除了改變程序打開文件本身,你的第二個解決方案是一個體面的方法。 – kenny 2010-06-02 09:29:22