C# 设置console和form同时显示

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

    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     }

 

 

 

你可能感兴趣的:(console)