How can I hide a console window?

前端 未结 4 501
小蘑菇
小蘑菇 2020-12-09 17:35

Question: I have a console program that shouldn\'t be seen. (It resets IIS and deletes temp files.)

Right now I can manage to hide the window right after start like

4条回答
  •  孤街浪徒
    2020-12-09 18:24

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("kernel32")]
        public static extern IntPtr GetConsoleWindow();
        [DllImport("Kernel32")]
        private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
    
        static void Main(string[] args)
        {
            IntPtr hConsole = GetConsoleWindow();
            if (IntPtr.Zero != hConsole)
            {
                ShowWindow(hConsole, 0); 
            }
        }
    

    This should do what your asking.

提交回复
热议问题