Console.Write Not Working In Win Forms App

前端 未结 7 1263
走了就别回头了
走了就别回头了 2021-01-12 05:08

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).

What am I d

7条回答
  •  不要未来只要你来
    2021-01-12 05:22

    The solution is:

    Declare Function AttachConsole Lib "kernel32.dll" (ByVal dwProcessId As Int32) As Boolean
    Declare Function FreeConsole Lib "kernel32.dll" () As Boolean
    

    [code.....]

    when needed the console output you just call AttachConsole(-1) but remember to do a FreeConsole() at the end of your program.

    the only problem with this solution is that you will read something like this:

    C:>yourapp.exe
    C:>Hello World!

    That's because a forms application runs as a child of the command prompt so the prompt returns immediately after you type the app name..

    In a console application the command prompt returns after the program exits.

    I am still trying to find a way to have the same behaviour (sync run) as in a console application.

提交回复
热议问题