Loading

[转]C#之winform控制台打印输出、打印调试

原文链接:C#之winform控制台打印输出、打印调试——CSDN

在Qt中,经常使用qDebug()<<"Hello World";的方式往控制台打印一些输出,以便观察程序的运行情况。

在Java中(eclipse、myeclipse中),经常使用的是System.out.println("Hello World");的方式。

在Android中,经常使用的是Log.d("Hello World");

. . .

在C#的时候,使用的是Console.WriteLine("Hello World");

开发winform的时候,需要先往主函数所在的源文件中加入以下内容。

引入库:

using System.Runtime.InteropServices;

在Main()前,添加:

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

在Main()中,第一行添加:

AllocConsole();

在Main()中,最后一行添加:

FreeConsole();

示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
// 控制台输出,需加入此库
using System.Runtime.InteropServices;

namespace HelloWorld_WindowsForm
{
    static class Program
    {
        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();
        [DllImport("kernel32.dll")]
        static extern bool FreeConsole();

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            // 允许调用控制台输出
            AllocConsole();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LoginPage());

            // 释放
            FreeConsole();
        }
    }
}
posted @ 2020-03-29 22:22  二次元攻城狮  阅读(3031)  评论(0编辑  收藏  举报