Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimize Console at app startup C#

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!

like image 872
Coder123 Avatar asked Jun 06 '26 12:06

Coder123


1 Answers

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();
    }
}
like image 97
KeithN Avatar answered Jun 09 '26 03:06

KeithN