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

时间:2011-04-18 17:44:04

标签: c# .net winforms console-application

我创建了一个Windows窗体解决方案,并在一个名为

的类的构造函数中

Console.WriteLine("constructer called")

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

4 个答案:

答案 0 :(得分:54)

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

答案 1 :(得分:45)

你还应该考虑使用Debug.WriteLine,这可能就是你要找的东西。这些语句是为您的应用程序写出的跟踪侦听器,可以在Output Window of Visual Studio

中查看
Debug.WriteLine("constructor fired");

答案 2 :(得分:15)

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

  

调试 - > Windows - >输出

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

答案 3 :(得分:1)

正如其他答案所述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

相关问题