C#在winform中调用系统控制台输出

在Winform程序中有时候调试会通过Console.Write()方式输出一些信息,这些信息是在Visual Studio的输出窗口显示。

所以就会想,能不能调用系统的Cmd窗口输出呢,经过一番查阅,发现是可以的,现在就把方法写下了:

主要用到的是win32 API函数实现的:

[DllImport("kernel32.dll")]
static extern bool FreeConsole();
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();

在Program.cs文件中调用方法即可

完整代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace XY.WinformDebug
    {
    static class Program
    {
            [DllImport("kernel32.dll")]
            static extern bool FreeConsole();
            [DllImport("kernel32.dll")]
            public static extern bool AllocConsole();
            ///
            /// 应用程序的主入口点。
            ///
            [STAThread]
            static void Main()
            {
                AllocConsole();//调用系统API,调用控制台窗口
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmMain());
                FreeConsole();//释放控制台
            }
        }
    }
updatedupdated2024-12-092024-12-09