29

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?

Yusubov
  • 5,815
  • 9
  • 32
  • 69
menacheb
  • 475
  • 1
  • 6
  • 17

9 Answers9

55

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

driis
  • 161,458
  • 45
  • 265
  • 341
  • Not really. Why change the whole baseline just not to show a console window? Not the best idea. – Nayan May 04 '10 at 08:32
  • @Nayan: What 'baseline' are you talking about? It's simply changing the "Output type" in the project properties. Takes two seconds flat. – Allon Guralnek May 04 '10 at 09:24
  • 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 May 04 '10 at 15:04
  • 5
    @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 May 04 '10 at 15:31
  • @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. – Nayan May 04 '10 at 15:41
15

On ProjectProperties set Output Type as Windows Application.

Nico.S
  • 753
  • 8
  • 12
5

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

peSHIr
  • 6,279
  • 1
  • 34
  • 46
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.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
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.

Quispie
  • 948
  • 16
  • 30
  • 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. – B.K. Nov 16 '15 at 06:32
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

Community
  • 1
  • 1
Nayan
  • 3,092
  • 25
  • 34
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

Ovidiu Pacurar
  • 8,173
  • 2
  • 30
  • 36
  • I have no idea if that works, but it sure reads out funny! :) Call ShowWindow() to hide it... ;) – noocyte May 04 '10 at 08:00
  • Instead of the hacky `FindWindow()` solution, I’d much prefer [retrieving the console handle properly](http://stackoverflow.com/questions/3571627)... – Timwi Aug 26 '10 at 02:30
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. ;)

-3

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

Archie
  • 2,564
  • 8
  • 39
  • 50
  • 1
    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. – ericosg Dec 06 '12 at 15:35