Console.Writeline () does not work

I am creating a C # winforms project that can be run as a graphical interface or can be controlled from the command line. I can currently handle input and command line arguments. I can run the program from the command line, and I can use the program to process the arguments. But Console.Writeline () does nothing. Any clue why this could be?

+6
command-line c # console
source share
7 answers

For the most part, see the answer here . However, I would like to point out the existence of a call to the FreeConsole() API, which allows you to gracefully close the console.

 [DllImport("kernel32.dll")] static extern int FreeConsole() 

One thing I would like to note: you can see some weird command line appearing before exiting the console if you start from an existing console and attach to it using AttachConsole (unlike AllocConsole ).

This is a temporary problem that is difficult to work with. If this is a problem, install the application as a console application, like the others. This will cause the command line to appear until the application closes, but it may not be the way you want it if you open winform.

In response to your comment : this is either an AttachConsole or an AllocConsole . The example I linked is trying to connect to an existing console first. If this fails (most likely because it does not exist), a new console window is created instead.

If you find a way to get the best of both worlds in terms of command line behavior and interactive GUI mode, please let me know. I haven’t been doing an in-depth search for a solution, but I have a few small applications that will benefit.

By the way, if you plan to use pipe on the command line (redirecting output to a file, for example), this will not work, as it is, unfortunately.
+6
source share

You can enable the console in a Windows forms application using the following DllImports:

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

Then you can enable the console using:

  AttachConsole(ATTACH_PARENT_PROCESS); 

And you can disable it using:

  FreeConsole(); 
+5
source share

This is because you wrote the winforms application - this means that System.Console.Out (i.e. the standard output stream) is set to Stream.Null . This means that any calls to this thread will fail.

You can handle input from the command line because they come from another thread. The moral of the story is that you can have a winforms application or a command line application, but not both at once.

+3
source share

I did this in a background thread that runs a function from a dynamically loaded DLL (reflection)

 AllocConsole(); IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput()); standardOutput.AutoFlush = true; Console.SetOut(standardOutput); // //Console writing here // FreeConsole(); 

My console was empty until I added Console.SetOut (), I'm not sure if you need to call GetStdHandle ()

+2
source share

You may need to change the type of application. This can be done either by changing it using the application properties in the graphical interface (as described in the answer from ConsultUtah), or you can edit the .csproj / STRONG file>

Winform / WPF:

 <OutputType>WinExe</OutputType> 

Console ( this is what you need for console.writeline() to work ):

 <OutputType>Exe</OutputType> 

Dll (including web applications):

 <OutputType>Library</OutputType> 
+1
source share

It is associated with the type of project, when you configure it on the "Application" tab in the project properties, you have 3 options:

  • Console application
  • Win application
  • Class library

If you are working with Win Application, however, you can also process command line arguments because they are received in the main function.

0
source share

The project must be configured to compile into a console application for the console to work. On the other hand, it will always have a console window even when launched as a graphical interface. I don't know about a workaround for this (but it would be interesting to hear about it, if any).

0
source share

All Articles