views:

430

answers:

6

Hi,
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?
Many thanks,

A: 

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
I have no idea if that works, but it sure reads out funny! :)Call ShowWindow() to hide it... ;)
noocyte
It does work though.
Şafak Gezer
Instead of the hacky `FindWindow()` solution, I’d much prefer [retrieving the console handle properly](http://stackoverflow.com/questions/3571627)...
Timwi
+12  A: 

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

driis
Good idea, I'll give it a try...
menacheb
This is probably the most common and easiest way to accomplish this.
David Neale
Not really. Why change the whole baseline just not to show a console window? Not the best idea.
Nayan
@Nayan: What 'baseline' are you talking about? It's simply changing the "Output type" in the project properties. Takes two seconds flat.
Allon Guralnek
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
@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
@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
+3  A: 

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

peSHIr
+1  A: 

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
A: 

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

Archie
+1  A: 

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 -

http://stackoverflow.com/questions/2647820/toggle-process-startinfo-windowstyle-processwindowstyle-hidden-at-runtime

and,

http://stackoverflow.com/questions/2636721/bring-another-processes-window-to-foreground-when-it-has-showintaskbar-false

Nayan