windows form .. console.writeline()在哪里是控制台?

Sma*_*EGA 44 .net c# console-application winforms

我创建了一个Windows窗体解决方案,并在我调用的类的构造函数中

Console.WriteLine("constructer called")

但我只得到表单而不是控制台..所以输出在哪里?

Tom*_*cek 57

在项目设置中将应用程序类型设置为Console.然后你将得到控制台和表格.


Bra*_*der 47

您还应该考虑使用Debug.WriteLine,这可能是您正在寻找的.这些语句是为应用程序编写的跟踪侦听器,可以在Visual Studio的" 输出"窗口中查看.

Debug.WriteLine("constructor fired");
Run Code Online (Sandbox Code Playgroud)

  • 我一直在尝试这个,但没有任何东西出现在输出窗口,除了通常的构建开始构建成功的东西. (4认同)

Alb*_*nbo 16

如果在Visual Studio中运行应用程序,则可以在输出窗口中看到控制台输出.

调试 - > Windows - >输出

请注意,从WinForms应用程序输出诊断数据的首选方法是使用System.Diagnostics.Debug.WriteLine或者System.Diagnostics.Trace.WriteLine因为它们可以更好地配置输出的方式和位置.


noe*_*cus 10

正如其他答案所述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();
Run Code Online (Sandbox Code Playgroud)

当打开某些开关时,我有时会使用它来引发命令提示符而不是应用程序窗口。

如果有人需要的话,在这个类似的问题中还有更多想法:
Winforms 中 Console.WriteLine() 的目的是什么