How to hide a console application in C#

2019-01-15 05:52发布

问题:

I have a console application in C#, and I want that the user won't be able to see it.

How can I do that?

回答1:

Compile it as a Windows Forms application. Then it won't display any UI, if you do not explicitly open any Windows.



回答2:

On ProjectProperties set Output Type as Windows Application.



回答3:

Sounds like you don't want a console application, but a windows GUI application that doesn't open a (visible) window.



回答4:

Create a console application "MyAppProxy" with following code, and put MyAppProxy in start up dir,

public static void main(string[] args)
{
   Process p = new Process("MyApp");
   ProcessStartUpInfo pinfo = new ProcessStartUpInfo();
   p.StartupInfo = pinfo;
   pinfo.CreateNoWindow = true;
   pinfo.ShellExecute = false;

   p.RaiseEvents = true;

   AutoResetEvent wait = new AutoResetEvent(false);
   p.ProcessExit += (s,e)=>{ wait.Set(); };

   p.Start();
   wait.WaitOne();
}

You may need to fix certain items here as I didnt check correctness of the code, it may not compile because some property names may be different, but hope you get the idea.



回答5:

The best way is to start the process without window.

        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "echo Hello!";
        //either..
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        //or..
        p.StartInfo.CreateNoWindow = true;
        p.Start();

See other probable solutions -

Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at runtime

and,

Bring another processes Window to foreground when it has ShowInTaskbar = false



回答6:

To hide a console applicatin in C# when nothing else works use this code:

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

Place FreeConsole() anywhere in the code, I placed it in the Init(), and the commandline is hidden.



回答7:

You can Pinvoke a call to FindWindow() to get a handle to your window and then call ShowWindow() to hide the window OR Start your application from another one using ProcessStartInfo.CreateNoWindow



回答8:

I've got a general solution to share:

using System;
using System.Runtime.InteropServices;

namespace WhateverNamepaceYouAreUsing
{
    class Magician
    {
    [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

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

        const int HIDE = 0;
        const int SHOW = 5;

        public static void DisappearConsole()
        {
            ShowWindow(GetConsoleWindow(), HIDE);
        }
    }
}

Just include this class in your project, and call Magician.DisappearConsole();.

A console will flash when you start the program by clicking on it. When executing from the command prompt, the command prompt disappears very shortly after execution.

I do this for a Discord Bot that runs forever in the background of my computer as an invisible process. It was easier than getting TopShelf to work for me. A couple TopShelf tutorials failed me before I wrote this with some help from code I found elsewhere. ;P

I also tried simply changing the settings in Visual Studio > Project > Properties > Application to launch as a Windows Application instead of a Console Application, and something about my project prevented this from hiding my console - perhaps because DSharpPlus demands to launch a console on startup. I don't know. Whatever the reason, this class allows me to easily kill the console after it pops up.

Hope this Magician helps somebody. ;)



回答9:

Create a wcf service and host it as per your need.