1. 定义一个Consolse帮助类,如下:
- public static class ConsoleHelper  
 
- {  
 
-     
 
-     
 
-     
 
-     
 
-     
 
-     
 
-     [DllImport("user32.dll", SetLastError = true)]  
 
-     private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
 
-   
 
-     
 
-     
 
-     
 
-     
 
-     
 
-     
 
-     [DllImport("user32.dll", SetLastError = true)]  
 
-     private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);  
 
-   
 
-     
 
-     
 
-     
 
-     
 
-     public static void hideConsole(string ConsoleTitle = "")  
 
-     {  
 
-         ConsoleTitle = String.IsNullOrEmpty(ConsoleTitle) ? Console.Title : ConsoleTitle;  
 
-         IntPtr hWnd = FindWindow("ConsoleWindowClass", ConsoleTitle);  
 
-         if (hWnd != IntPtr.Zero)  
 
-         {  
 
-             ShowWindow(hWnd, 0);  
 
-         }  
 
-     }  
 
-   
 
-     
 
-     
 
-     
 
-     
 
-     public static void showConsole(string ConsoleTitle = "")  
 
-     {  
 
-         ConsoleTitle = String.IsNullOrEmpty(ConsoleTitle) ? Console.Title : ConsoleTitle;  
 
-         IntPtr hWnd = FindWindow("ConsoleWindowClass", ConsoleTitle);  
 
-         if (hWnd != IntPtr.Zero)  
 
-         {  
 
-             ShowWindow(hWnd, 1);  
 
-         }  
 
-     }  
 
- }  
 
 
2. 程序中调用,如下:
- ConsoleHelper.hideConsole();  
 
 
3. 注意:如果程序是只能启动一个,则可以用上面的方法控制控制台的显示与隐藏;否则需要在初始化时对控制台的标题赋值,如下:
- Console.Title = Guid.NewGuid().ToString();  
 
 C# 控制台窗口的显示与隐藏
原文:http://www.cnblogs.com/aiqingqing/p/4556465.html