I have a Console Application app, I would like to minimize (not hide permanently) the Console when i run the app, is it possible? also, I am using a timer to run the task every 10 minutes, is it possible to minimize the console every time the app runs? thanks!
Asked
Active
Viewed 1.1k times
7
-
Possible duplicate? [.Net Console Application in System tray](https://stackoverflow.com/questions/751944/net-console-application-in-system-tray) – P. Zoltan Jun 21 '17 at 11:43
-
2I dont want to put it the the system tray. I just want it minimized – Coder123 Jun 21 '17 at 11:46
1 Answers
20
The following code should do the trick. Uses Win32 methods to minimize the console window. I am using Console.ReadLine() to prevent the window closing immediately.
internal class Program
{
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow([In] IntPtr hWnd, [In] int nCmdShow);
private static void Main(string[] args)
{
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(handle, 6);
Console.ReadLine();
}
}

KeithN
- 414
- 1
- 6
- 7
-
-
-
1Gotta say this out loud - I mistakenly downvoted this answer for absolute no reason (didnt realize until saw colored down arrow today). I apologize for this, as I can't fix that now (21 hours pass, my vote is locked), but it should have been an upvote, since this answer works just like it should!!! – That Marc Aug 19 '20 at 13:42
-
3To clarify, the `6` corresponds to `SW_MINIMIZE` according to https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-showwindow?WT.mc_id=DOP-MVP-5001655 – David Gardiner Oct 17 '22 at 04:51