有时我们需要在窗体应用程序中同时启动控制台显示我们的程序运行情况等等,这个时候我们有两种解决方式:

    1.项目->属性->应用程序->输出类型->控制台应用程序。

    2.在form的Program.cs文件添加如下代码:

    

 1     static class Program
 2     {
 3         /// <summary>
 4         /// 应用程序的主入口点。
 5         /// </summary>
 6         [STAThread]
 7         static void Main()
 8         {
 9 
10             ConsoleEx.AllocConsole();
11 
12             Application.EnableVisualStyles();
13             Application.SetCompatibleTextRenderingDefault(false);
14             Application.Run(new Main());
15         }
16     }
17     class ConsoleEx
18     {
19         /// <summary>
20         /// 启动控制台
21         /// </summary>
22         [DllImport("kernel32.dll")]
23         public static extern bool AllocConsole();
24 
25         /// <summary>
26         /// 释放控制台
27         /// </summary>
28         [DllImport("kernel32.dll")]
29         public static extern bool FreeConsole();
30     }

 

 

 

相关文章:

  • 2021-05-08
  • 2022-12-23
  • 2021-08-05
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
猜你喜欢
  • 2022-12-23
  • 2021-12-21
  • 2021-11-16
  • 2021-10-08
  • 2021-04-12
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案