Hide Command Window in C# Application

前端 未结 4 1456
温柔的废话
温柔的废话 2020-12-10 03:50

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:

  proc         


        
相关标签:
4条回答
  • 2020-12-10 03:55

    i see that you are calling cmd and then passing the command as parameters. Instead call the command directly

    e.g.

        System.Diagnostics.ProcessStartInfo procStartInfo = new System.DiagnosticsProcessStartInfo("xcopy", "<sourcedir> <destdir> <other parameters>");
    
    procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    
    0 讨论(0)
  • 2020-12-10 04:13

    I had a similar task - It is possible to hide the window after creation via an API call. (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

    To get the handle of the Window take a look at:

    pinvoke.net enumwindows(user32)

    0 讨论(0)
  • 2020-12-10 04:14

    You can try adding

    process.StartInfo.UseShellExecute = false; 
    

    to your process

    0 讨论(0)
  • 2020-12-10 04:19

    The problem is that you're using 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. Like COPY.

    You can still suppress the window if you need cmd.exe, you'll have to use the /B option for Start. Type start /? at the command prompt to see options. Not that it helps, you can't use START COPY.

    There's a specific quirk in xcopy.exe that might throw you off as well. It does not execute if you don't also redirect the input. It just fails to run without diagnostic.

    0 讨论(0)
提交回复
热议问题