如何在 C# 中将文件名传递给 StandardInput(进程)?

发布于 2024-09-04 01:44:30 字数 1237 浏览 5 评论 0原文

我正在从命令行使用本机 Windows 应用程序 spamc.exe (SpamAssassin - sawin32),如下所示:

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 命令行中那样传递文件名则不起作用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昔日梦未散 2024-09-11 01:44:30

这是因为在命令行上 < 是一个小小的神奇参数。它只是比你想象的多一点点。事实上,它打开文件并将其内容放入进程的标准输入中。因此,这与使用 Process 类时必须手动执行的操作相同。

正如您在第二个示例中所示,您必须使用 StreamReader 来获取文件的内容并将其放入 StandardInput 中。为了使其更加健壮,您可以使用这个小代码片段:

using (var streamReader = new StreamReader(fileInfo.FullName))
{
    process.StandardInput.Write(streamReader.ReadToEnd());
    process.StandardInput.Flush();
}

It's cause on the command line the < is a little magic parameter. It just does a little bit more, then you maybe expect. In fact it opens the file and put its content into the standard input of the process. So that's the same you must do manually when using the Process class.

As you already showed in your second example you have to use a StreamReader to get the content of the file and put it into the StandardInput. Just to make it a little more robust you can maybe use this little code snippet:

using (var streamReader = new StreamReader(fileInfo.FullName))
{
    process.StandardInput.Write(streamReader.ReadToEnd());
    process.StandardInput.Flush();
}
久光 2024-09-11 01:44:30

在第一个示例中,您传递的字符串表示文件而不是文件。

In the first example you are passing a string that represents the file not the file.

眼泪也成诗 2024-09-11 01:44:30

您的第一个代码示例相当于从包含行 C:\email.eml 的文件中定向输入:

echo C:\email.eml > inputfile
C:\SpamAssassin\spamc.exe -R < inputfile

您的第二个代码示例传递 C:\email.eml 的内容> 到spamc

Your first code sample is the equivalent of directing input from a file containing the line C:\email.eml:

echo C:\email.eml > inputfile
C:\SpamAssassin\spamc.exe -R < inputfile

Your second code sample passes the content of C:\email.eml to spamc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文