WinForms应用程序:console.writeline()输出呈现在哪里?

rfbsl7qr  于 2023-01-09  发布在  其他
关注(0)|答案(4)|浏览(535)

我创建了一个windows窗体解决方案,并在我调用的类的构造函数中
第一个月
但是我只得到了表单而没有控制台..那么输出在哪里呢?

qncylg1j

qncylg1j1#

你也应该考虑使用Debug.WriteLine,这可能就是你所寻找的,这些语句是为你的应用程序编写的跟踪侦听器,可以在Output Window of Visual Studio中查看。

Debug.WriteLine("constructor fired");
34gzjxbg

34gzjxbg2#

在项目设置中设置应用程序类型为控制台。然后你会得到控制台窗口和Windows窗体。

4nkexdtk

4nkexdtk3#

如果在Visual Studio中运行应用程序,则可以在输出窗口中看到控制台输出。
调试-〉窗口-〉输出
请注意,从WinForms应用程序输出诊断数据的首选方法是使用System.Diagnostics.Debug.WriteLineSystem.Diagnostics.Trace.WriteLine,因为它们更易于配置输出的方式和位置。

mi7gmzs6

mi7gmzs64#

正如其他人回答的那样,System.Diagnostics.Debug.WriteLine是调试消息的正确方法。
从Winforms应用程序中,您可以调用控制台窗口进行如下交互:

using System.Runtime.InteropServices;

...

void MyConsoleHandler()
{
    if (AllocConsole())
    {
        Console.Out.WriteLine("Input some text here: ");
        string UserInput = Console.In.ReadLine();

        FreeConsole();
    }
}

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeConsole();

我有时使用它来打开命令提示符,而不是打开应用程序窗口。
如果有人需要的话,这个类似的问题还有更多的想法:
What is the Purpose of Console.WriteLine() in Winforms

相关问题