Console.Writeline() not working

26,923

Solution 1

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

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

One thing I'd like to note: you may see some weirdness of a command prompt appearing in front of your console output, if you're launching from an existing console and attaching to that with AttachConsole (as opposed to AllocConsole).

This is a timing issue which is hard to work around. If that's a problem, set your application to be a Console application like the others suggested. It will have the effect of no command prompt appearing until the application closes however, which might not be what you want if you're opening a winform.

In response to your comment: it's either AttachConsole or AllocConsole. The example I linked tries to attach to an existing console first. If that fails (most likely because it doesn't exist), it creates a new console window instead.

If you find a way to have the best of both worlds in terms of command-line behavior and GUI interactive mode, please let me know. I haven't done any in-depth searching for a solution, but I have a few minor apps that would benefit.

By the way: if you plan on using pipes on your command line (redirecting output to a file for example), that won't work like this unfortunately.

Solution 2

You can enable the console in a windows forms app 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();

You can then enable the console using:

    AttachConsole(ATTACH_PARENT_PROCESS);

And you can disable it using:

     FreeConsole();

Solution 3

I did this in a background thread that is running 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 blank until I added the Console.SetOut(), I am not sure you need to call GetStdHandle()

Solution 4

This is because you have written a winforms app - this means that System.Console.Out (i.e. the standard output stream) is set to Stream.Null. This means that any calls to that stream will silently fail.

You are able to process input from the command line because they come from a different stream. The moral of the story is that you can have a winforms app, or a command line app, but not both at once.

Solution 5

You might need to change the application type. This can be done by either changing it via Application Properties in the GUI (as explained in the answer from ConsultUtah) or you can edit the .csproj file:

Winform/WPF:

<OutputType>WinExe</OutputType>

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

<OutputType>Exe</OutputType>

Dll (including web applications):

<OutputType>Library</OutputType>
Share:
26,923
novacara
Author by

novacara

Updated on July 09, 2022

Comments

  • novacara
    novacara almost 2 years

    I'm creating a c# winforms project that can be run as a GUI or can be operated from the command line. Currently, I can process command line input and arguments. I can run the program from command line and I can use the program to process the arguments. But Console.Writeline() does absolutely nothing. Any clue why that could be?

  • Thorarin
    Thorarin almost 15 years
    Yes, sort of... Windows API calls to the rescue :)
  • novacara
    novacara almost 15 years
    This solution works great. Fortunately I don't need pipes. Thank you!!
  • Emile Vrijdags
    Emile Vrijdags almost 9 years
    Thanks, I had an AllocConsole which was working for some time, and suddenly it didn't anymore. Adding the Console.SetOut() did the trick!
  • HerrimanCoder
    HerrimanCoder about 4 years
    This answer should be deleted because of the inaccurate statement: "The moral of the story is that you can have a winforms app, or a command line app, but not both at once." gls123 disproves it.
  • DAG
    DAG about 3 years
    Wow, there are a lot of answers on how to fix this problem. Yours worked after I reviewed the past dozen. I'm using kernel32 "attachconsole". Needed the new streamwriter, the autoflush to true, and the SetOut.