Making my console application invisible

匆匆过客 提交于 2019-11-29 16:39:54

问题


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.


回答1:


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.




回答2:


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 int SW_HIDE=0;
    const int 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.




回答3:


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




回答4:


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




回答5:


Hello I was creating a Console application to be called by the task scheduler. I didn't want the console app to show up so I changed the project properties to have the output to Windows Application.

Change the output type to Windows application Go to : Project - >Project Properties And change the output type to Windows Application




回答6:


I tried both methods 2) Searock and then 1) Josh --- with Searock's solution the console app window still appeared, although for a very brief moment --- however with Josh's solution the console did not appear nor did my program have any issues -- of course I did have to replace all the console.writeline calls with a call that logged the information out to a log file

Note: I would have just commented on Josh's solution but I cannot do that quite yet :)



来源:https://stackoverflow.com/questions/3497652/making-my-console-application-invisible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!