views:

155

answers:

4

I am developing a console application for my public library as a school project. The console application will run as soon as the user logs on and do some background work.

The thing is, I don't want the console application to actually appear. I need it invisible. The last thing I need is complaints because some people got freaked out that a CMD window opened and closed, besides that the library wants it as invisible as possible.

I tried following the code in this thread: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd

but to no avail, I can still see the console application pop open and close after it has done all of its work.

Is there a better way to stop the console from appearing? Thanks.

+10  A: 

The best thing to do is just don't compile it as a console application! Compile it as a Windows EXE and no console will show. Then you can just do whatever you need to do in the Main method without showing a UI.

But in any case, if you must hide/show the console window I would steer clear of using FindWindow for this task since there is a much more reliable API for this: GetConsoleWindow. This will give you the HWND of the console window and you can try passing that to ShowWindow.

Josh Einstein
That was going to be my suggestion as well. +1.
David Stratton
+! for intuitive, Microsoft style...
NinjaCat
Ah, I see. So what would that be under "New Project" in Visual Studio? "Empty Project"? 'Cause I don't see an option for a normal .exe, just some forms and a console application.
Kratz
You can just right click the project, choose properties, then change the output type from Console Application to Windows Application. No other change should be necessary.
Josh Einstein
Okay, I did that but now my application won't work :/ When it was a console application it used to make directories and send a web request... now it won't do any of that.
Kratz
I can't think of any reason that simply changing the output type would cause any of that. Perhaps you changed something else while trying to hide the console window?
Josh Einstein
+1  A: 

Have you tried: Project Properties> Application > output Type: to "Windows Application"?

NinjaCat
I just did that and now the application is broken, it is meant to make directories and copy files from an Embedded Resource and now it doesn't work. :(
Kratz
A: 

Its a little more complicated than a console application... but if you want something to truly be running in the background when someone logs in then you could create a Windows Service application.

But it does require a little additional work in setting up and installing the Windows Service but there is an abundance of example code on the web:

http://msdn.microsoft.com/en-us/library/9k985bc9(v=VS.80).aspx

http://msdn.microsoft.com/en-us/library/sd8zc8ha(v=VS.80).aspx

http://www.c-sharpcorner.com/uploadfile/mahesh/window_service11262005045007am/window_service.aspx

http://www.developer.com/net/net/article.php/2173801/Creating-a-Windows-Service-in-NET.htm

http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

Lewray
+1  A: 

As Josh Einstein has suggested you can use ShowWindow Api to hide your window.

Here's a example:

using System.Runtime.InteropServices

class CommandLine
{

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

    [DllImport("Kernel32")]
    private static extern IntPtr GetConsoleWindow();

    const SW_HIDE=0;
    const SW_SHOW=5;

    static void Main(string[] args)
    {
         IntPtr hwnd;
         hwnd=GetConsoleWindow();
         ShowWindow(hwnd,SW_HIDE);

         //Your logic goes here
    }
}

I am not sure about this code as I have not tested it. Let me know if you face any problem.

Searock