ProcessWindowStyle.Hidden 的进程仍然显示按任意键退出?
我有这个:
var startInfo = new ProcessStartInfo
{
FileName = _pathToExe,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(_pathToExe),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
try
{
using (_proc = Process.Start(startInfo))
{
_proc.EnableRaisingEvents = true;
_proc.ErrorDataReceived += proc_DataReceived;
_proc.OutputDataReceived += proc_DataReceived;
_proc.BeginErrorReadLine();
_proc.BeginOutputReadLine();
var myStreamWriter = _proc.StandardInput;
var allArgs = "";
foreach (var arg in _args)
allArgs += arg + Environment.NewLine;
myStreamWriter.Write(allArgs);
_proc.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
我正在执行别人编写的 *.exe。对于这个特定的 *.exe - 即使您可以在上面清楚地看到我设置了 ProcessWindowStyle.Hidden,我仍然看到一个黑色窗口出现,上面写着“按任意键退出。”。这个 *.exe - 如果我从命令行运行(而不是从我的 C# 代码调用它)会生成大量控制台输出文本。当我运行 C# 代码时,我没有看到此输出文本,这正是我想要的,也意味着重定向正在工作。
我检查了一下,该过程已完成 - 就好像命令窗口本身正在添加这个额外的(不需要的)步骤。
以前有人遇到过这种情况吗?如果是的话我该如何摆脱它?
I have this:
var startInfo = new ProcessStartInfo
{
FileName = _pathToExe,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(_pathToExe),
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
try
{
using (_proc = Process.Start(startInfo))
{
_proc.EnableRaisingEvents = true;
_proc.ErrorDataReceived += proc_DataReceived;
_proc.OutputDataReceived += proc_DataReceived;
_proc.BeginErrorReadLine();
_proc.BeginOutputReadLine();
var myStreamWriter = _proc.StandardInput;
var allArgs = "";
foreach (var arg in _args)
allArgs += arg + Environment.NewLine;
myStreamWriter.Write(allArgs);
_proc.WaitForExit();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
And I am executing an *.exe that someone else wrote. For this particular *.exe - even though you can clearly see above that I have set ProcessWindowStyle.Hidden, I still see a black window appear with the words "Press any key to exit.". This *.exe - if I run from the command line (instead of calling it from my C# code) produces a tremendous amount of console output text. I do not see this output text when I run my C# code, which is what I want and also means the redirection is working.
I checked and the process is finished - it's as if the command window itself is adding this extra (undesirable) step.
Has anyone encountered this before and if so how can I get rid of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在启动的程序在最后调用了 system("PAUSE") ,从而产生了一个新进程,该进程打印“按任意键继续...”消息并等待用户输入。我无法重现您的确切情况,但您可以尝试一下。
It seems that the program you are starting is calling system("PAUSE") at the end thus spawning a new process which prints the "Press Any Key to Continue..." message and waits for user input. I cannot reproduce the exact situation of yours but you can try this.
来自文档:
要使用 System.Diagnostics.ProcessWindowStyle.Hidden,system.Diagnostics.ProcessStartInfo.UseShellExecute 属性必须为 true。
From Document:
To use System.Diagnostics.ProcessWindowStyle.Hidden, the system.Diagnostics.ProcessStartInfo.UseShellExecute property must be true.