7

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!

Coder123
  • 784
  • 2
  • 8
  • 29
  • 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
  • 2
    I dont want to put it the the system tray. I just want it minimized – Coder123 Jun 21 '17 at 11:46

1 Answers1

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
  • used different method, but your answer helped me – Coder123 Jun 21 '17 at 12:07
  • It would be nice to feedback and say what method you did use – georgeok Nov 07 '19 at 16:05
  • 1
    Gotta 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
  • 3
    To 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