隐藏进程窗口,为什么不起作用?

问题描述 投票:0回答:3

我现在已经尝试了几种方法来隐藏新进程的窗口(在本例中它只是用于测试的 notepad.exe),但无论我尝试什么,它都不起作用。

我读过很多帖子,现在都说同样的话,为什么它对我不起作用?

我有一个控制台应用程序,应该启动其他进程而不显示其窗口。

我尝试让我的控制台应用程序在没有窗口的情况下启动 notepad.exe,但它不起作用。

ProcessStartInfo info = new ProcessStartInfo("path to notepad.exe");

info.RedirectStandardOutput = true;
info.RedirectStandardError = true;                                
info.CreateNoWindow = true;
info.UseShellExecute = false;                                

Process proc = Process.Start(info);

我还尝试使用 info.WindowStyle 的各种设置,并且尝试将我的控制台应用程序配置为 Windows 应用程序,但我做什么并不重要,子进程总是打开一个窗口。

控制台应用程序不允许这样做吗?或者这里有什么问题 - 任何人都可以阐明这一点吗?

我在 Windows 7 x64 上使用 .NET 4.0

c# windows console process.start windowless
3个回答
20
投票

根据我的经验,每当我启动“cmd.exe”时,以下内容都会起作用。

info.CreateNoWindow = true;
info.UseShellExecute = false;                                

它似乎不适用于“notepad.exe”。其他应用程序也会失败,例如“excel.exe”和“winword.exe”。

但是,这有效:

ProcessStartInfo info = new ProcessStartInfo("notepad.exe");

info.WindowStyle = ProcessWindowStyle.Hidden;

Process proc = Process.Start(info);

来自MSDN

窗口可以是可见的,也可以是隐藏的。系统通过不绘制隐藏窗口来显示它。如果窗口被隐藏,则它实际上被禁用。隐藏窗口可以处理来自系统或其他窗口的消息,但不能处理用户的输入或显示输出。通常,应用程序可能会在自定义窗口外观时隐藏新窗口,然后将窗口样式设置为Normal。要使用 ProcessWindowStyle.Hidden,ProcessStartInfo.UseShellExecute 属性必须为 false

我测试的时候,不用设置

UseShellExecute = false


0
投票

info.CreateNoWindow = true;

上面的行将阻止在 c# 中显示
我使用下面的代码在后台运行 exe,并读取 exe 的输出:

string ExeName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Curl.exe");
ProcessStartInfo Info = new ProcessStartInfo();
Info.UseShellExecute = false;
Info.FileName = ExeName;
Info.CreateNoWindow = true;
Info.Arguments = 参数;
Info.RedirectStandardOutput = true;
Info.WindowStyle = ProcessWindowStyle.Hidden;
进程 P = Process.Start(Info);
字符串OutPut = P.StandardOutput.ReadToEnd();
返回输出;


0
投票

现在我有一个特别问题。

我知道可以编辑进程的运行选项(使用 ProcessStartInfo)

但是当进程已经在运行时怎么办? 等等。我不想创建 3 个按钮,这将使用当前进程进行操作,例如,我将标签放入表单,然后在其中输入一些我不想操作他的随机过程。

当表单启动时,也会有 3 按钮 单击按钮时,当前进程将最小化、标准化或最大化。

我做了很多搜索,但没有找到任何东西..

但是我尝试了这个方法的顺便说一句(这对我来说是有道理的)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each pr As Process In Process.GetProcesses
        If pr.ProcessName = Label1.Text Then
            pr.MainWindowHandle = System.Diacnostics.ProcessWindowStyle.Maximalized 'Or Normal and Minimalize
        End If
    Next
End Sub

然后我也尝试了这个:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Program As Process = Process.GetProcessesByName(Label1.Text)
    If Program.Length > 0 Then
        AppActivate(Program(0).Id)
        Program(0).MainWindowHandle = System.Diagnostics.ProcessWindowStyle.Normal
    End If
End Sub

而且它不起作用..

所以,我真的不需要你的帮助,并且所有好的答案都将被要求;)

© www.soinside.com 2019 - 2024. All rights reserved.