简体   繁体   English

在 C# 应用程序中隐藏命令窗口

[英]Hide Command Window in C# Application

Before you say its a duplicate question, please let me explain (as I've read all similar threads).在你说它是一个重复的问题之前,请让我解释一下(因为我已经阅读了所有类似的主题)。

My application has both of these settings:我的应用程序具有以下两种设置:

  procStartInfo.CreateNoWindow = true;
  procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

and is also has WindowsApplication as the output type.并且还有 WindowsApplication 作为输出类型。

The black window STILL comes up when I call a command line command.当我调用命令行命令时,黑色窗口仍然出现。 Is there anything else I can do to hide the window?我还能做些什么来隐藏窗口吗? It doesn't happen for all commands, XCOPY is a situation where it the black window does flash up.并非所有命令都发生这种情况,XCOPY 是黑色窗口确实闪烁的情况。 This only happens though when the destination I'm XCOPYing too already contains the file and it's prompting me if I want to replace it.这只发生在我 XCOPY 的目的地也已经包含该文件并且它提示我是否要替换它时。 Even if I pass in /Y it will still flash briefly.即使我传入 /Y 它仍然会短暂闪烁。

I'm open to using vbscript if that will help, but any other ideas?如果有帮助,我愿意使用 vbscript,但还有其他想法吗?

The client will call my executable and then pass in a command line command ie:客户端将调用我的可执行文件,然后传入命令行命令,即:

C:\MyProgram.exe start XCOPY c:\Test.txt c:\ProgramFiles\

Here's the full code of the application:这是应用程序的完整代码:

class Program
{
    static void Main(string[] args)
    {      
            string command = GetCommandLineArugments(args);

            // /c tells cmd that we want it to execute the command that follows and then exit.
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);

            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;

            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = procStartInfo;
            process.Start();

        }

    private static string GetCommandLineArugments(string[] args)
    {
        string retVal = string.Empty;

        foreach (string arg in args)
            retVal += " " + arg;


        return retVal;
    }
}

The problem is that you're using cmd.exe.问题是您使用的是 cmd.exe。 Only its console window will be hidden, not the console window for the process you ask it to start.只有它的控制台窗口将被隐藏,而不是您要求它启动的进程的控制台窗口。 There's little point in using cmd.exe, unless you are trying to execute some of the commands it implements itself.使用 cmd.exe 没有什么意义,除非您尝试执行它自己实现的一些命令。 Like COPY.像复制一样。

You can still suppress the window if you need cmd.exe, you'll have to use the /B option for Start.如果您需要 cmd.exe,您仍然可以抑制窗口,您必须使用 /B 选项来启动。 Type start /?输入开始/? at the command prompt to see options.在命令提示符下查看选项。 Not that it helps, you can't use START COPY.并不是说它有帮助,您不能使用 START COPY。

There's a specific quirk in xcopy.exe that might throw you off as well. xcopy.exe 中有一个特定的怪癖可能会让你失望。 It does not execute if you don't also redirect the input.如果您不重定向输入,它不会执行。 It just fails to run without diagnostic.它只是无法在没有诊断的情况下运行。

i see that you are calling cmd and then passing the command as parameters.我看到您正在调用cmd然后将命令作为参数传递。 Instead call the command directly而是直接调用命令

eg例如

    System.Diagnostics.ProcessStartInfo procStartInfo = new System.DiagnosticsProcessStartInfo("xcopy", "<sourcedir> <destdir> <other parameters>");

procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

You can try adding您可以尝试添加

process.StartInfo.UseShellExecute = false; 

to your process到你的过程

If you are calling cmd.exe in your C# code and passing the commands to it via standard input.WriteLine and you don't want your CMD window to pop up every time you run your code, you can simply write this command:如果您在 C# 代码中调用cmd.exe并通过标准input.WriteLine将命令传递给它,并且您不希望每次运行代码时都弹出 CMD 窗口,您可以简单地编写以下命令:

 test.StartInfo.FileName = "cmd.exe";
 test.StartInfo.CreateNoWindow = true;

By setting create no window to false, we are running the command sent to the CMD in the background and the output is not being displayed to the user.通过将create no window设置为 false,我们正在后台运行发送到 CMD 的命令,并且不会向用户显示输出。 By setting it to false, the CMD window pops up.通过将其设置为 false,将弹出 CMD 窗口。

I had a similar task - It is possible to hide the window after creation via an API call.我有一个类似的任务 - 可以通过 API 调用在创建后隐藏窗口。 (In your case you maybe need a helper thread.) (在您的情况下,您可能需要一个辅助线程。)

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

If you know the handle of the new Window you can call如果您知道新窗口的句柄,您可以调用

ShowWindow(hWnd, 0);

0 hides the window, 1 shows the window 0隐藏窗口,1显示窗口

To get the handle of the Window take a look at:要获取 Window 的句柄,请查看:

pinvoke.net enumwindows(user32) pinvoke.net enumwindows(user32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM