30

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?

6
  • 3
    is it a service that you want to run in the background? Commented May 4, 2010 at 7:56
  • Yes, but I don't want to run it AS service, I want to run it as an .exe Commented May 4, 2010 at 7:57
  • How would the application start? Commented May 4, 2010 at 8:00
  • Automaticly, from the StartUp dir. Commented May 4, 2010 at 8:01
  • There is also a way to show and hide the console window while the program is running. Commented Aug 26, 2010 at 2:31

9 Answers 9

56

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

Sign up to request clarification or add additional context in comments.

5 Comments

Not really. Why change the whole baseline just not to show a console window? Not the best idea.
@Nayan: What 'baseline' are you talking about? It's simply changing the "Output type" in the project properties. Takes two seconds flat.
I'm sure about the speed. It's the design change and assembly dependency and bloated executable due to unnecessary assembly reference and implementation of some abstract and interfaces I'm talking about. I'm talking about design-modification-without-thinking problem.
@Nayan, there need not be any design change. No one says you should implement any interfaces nor reference any assemblies. It is just switching the compile mode, which really just sets a flag in the assembly as "I am a console program" vs "I am a Windows program". In fact, you can compile as a Windows program without referencing anything else than mscorlib.
@driis: I agree with you, mostly. But my point was that it's not always so simple because a console program is supposed to run in console (and it may have access to GUI components), and vice-versa too, if you really want to complicate it. In the end, its the OP who is going to decide what actually works for him. I just provided my version of aesthetic coding practice.
15

On ProjectProperties set Output Type as Windows Application.

Comments

5

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

Comments

5

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.

Comments

2

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

Comments

2

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.

1 Comment

This appears to be a bit tricker than just doing that. When you detach the process from its console by using FreeConsole(), the console is terminated. You have to remember to terminate the process when the work is done, as there is no console to do it for you, unless I'm missing something (Iet me know if I do). Still, a very neat method.
1

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

2 Comments

I have no idea if that works, but it sure reads out funny! :) Call ShowWindow() to hide it... ;)
Instead of the hacky FindWindow() solution, I’d much prefer retrieving the console handle properly...
1

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. ;)

Comments

-3

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

1 Comment

I think this is misleading and completely in the wrong direction. The uses for a hidden app could be completely different to a wcf service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.