Console.Write不工作在Win Forms应用程序

问题描述:

我在Visual Studio 2008中创建了一个VB.NET窗体应用程序。当我从命令行运行我的程序,我没有输出(只有下一个提示)。

I created a VB.NET Windows Forms Application in Visual Studio 2008. When I run my program from the command-line, I get no output (only the next prompt).

我做错了什么?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Debug.Write("Foo")
    Debug.Flush()
    Console.WriteLine("foo")
    Console.Beep(800, 100) 'confirm this function is called'
    Me.Close()
End Sub

EDIT:程序可以有表单和控制台吗?

Can a program have a form and a console?

EDIT2:但是,输出将显示在下一个命令行提示符下。

Ho's answer works. However, the output appears on the next command-line prompt. Can a Winforms application tell the command-line to wait until it's finished instead of immediately returning?

或者如果你已经有一个命令行, WinForms应用程序可以使用AttachConsole API将控制台附加到它。

Or if you already have a WinForms application you can attach a Console to it using the AttachConsole API.

using System.Runtime.InteropServices;  

...

[DllImport("kernel32.dll")] static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;  

...

AttachConsole(ATTACH_PARENT_PROCESS);

(格式化为代码)