ProcessWindowStyle.Hidden 的进程仍然显示按任意键退出?

发布于 2025-01-01 15:57:19 字数 1705 浏览 0 评论 0原文

我有这个:

        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 技术交流群。

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

发布评论

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

评论(2

洒一地阳光 2025-01-08 15:57:19

看来您正在启动的程序在最后调用了 system("PAUSE") ,从而产生了一个新进程,该进程打印“按任意键继续...”消息并等待用户输入。我无法重现您的确切情况,但您可以尝试一下。

        var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe", 
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
        };

        try
        {
            Process _proc;
            using (_proc = Process.Start(startInfo))
            {
                _proc.EnableRaisingEvents = true;
                _proc.BeginErrorReadLine();
                _proc.BeginOutputReadLine();

                var myStreamWriter = _proc.StandardInput;

                myStreamWriter.WriteLine("D:\\your.exe"); //write your.exe to cmd and press enter :) 
                _proc.WaitForExit();

            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

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.

        var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe", 
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
        };

        try
        {
            Process _proc;
            using (_proc = Process.Start(startInfo))
            {
                _proc.EnableRaisingEvents = true;
                _proc.BeginErrorReadLine();
                _proc.BeginOutputReadLine();

                var myStreamWriter = _proc.StandardInput;

                myStreamWriter.WriteLine("D:\\your.exe"); //write your.exe to cmd and press enter :) 
                _proc.WaitForExit();

            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
镜花水月 2025-01-08 15:57:19

来自文档:

要使用 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.

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