14

Hi I know how to write to console but if I write to console in my program and call my program from the command line it won't display anything.

How do I make it so that when I say Console.WriteLine or Console.Out.Writeline ir prints to the command prompt from which it was called and not somewhere else?

Once again I know how to do Console.WriteLine so it's not that :-p unless I'm doing it wrong.

From what I can tell it's probably something to do with Console.SetOut(TextWriter t)

this is a WPF application and I need it to post its data to the command line while still retaining the GUI at startup. I've triple checked and my code hits the print lines, I can actually see the lines being printed to the Visual Studio output window, it just won't display in the command line when I run it manually without VS.

If possible I need to conditionally have the console display. ie if run from command line (or even with command arguments), display or post to the prompt, otherwise do not.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
ben
  • 155
  • 1
  • 1
  • 6
  • 1
    Can you add some more code please? – Preet Sangha Jun 16 '10 at 23:17
  • That should work, need more info/code – µBio Jun 16 '10 at 23:21
  • okay. I want to print out a string with help information to the command line, held in my string "helpString". Console.WriteLine(helpString); this will not output the string to the command prompt if I open it up, navigate to my executable, and run it. So far as I know, it must be printing the output elsewhere. So I tried using the Console class' SetOut method but I don't know where to get a StreamWriter which would apply to the command prompt. there really isn't more code than that. This is a WPF project in case that helps. – ben Jun 16 '10 at 23:22
  • possible duplicate of [No output to console from a WPF application?](http://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application) – Preet Sangha Jun 16 '10 at 23:28
  • That's the same question but it isn't resolved because I would like to not create a new command prompt but rather post the output to the command prompt which launched the executable conditionally if it has arguments or to not even bother with the console if there are none. – ben Jun 16 '10 at 23:33

3 Answers3

34

This is actually trivial:

public void WriteToConsole(string message)
{
  AttachConsole(-1);
  Console.WriteLine(message);
}
[DllImport("Kernel32.dll")]
public static extern bool AttachConsole(int processId);

This method will write your message to the console if your program was started from the command line, otherwise it will do nothing.

If you want to use an alternative output mechanism when you weren't started from the command line you can do it this way:

public void WriteToConsole(string message)
{
  _connected = _connected || AttachConsole(-1);
  if(_connected)
    Console.WriteLine("Hello");
  else
    ... other way to output message ...
}
bool _connected;
[DllImport("Kernel32.dll")]
public static extern bool AttachConsole(int processId);
Lefty
  • 166
  • 1
  • 12
Ray Burns
  • 62,163
  • 12
  • 140
  • 141
  • 4
    Nice. I had a small issue where the console window (session?) doesn't exit when run from the command line. Adding this took care of it: "SendKeys.SendWait ("{ENTER}");". I also added a call to FreeConsole(). – Scott Marlowe Apr 04 '11 at 01:01
  • 6
    This is cool, but how do you get it so that the command line prompt comes back? what I get is the prompt, then the console starts to be written, so the end user isn't left with the c:\> prompt. – Richard B Mar 21 '13 at 15:22
4

The full code for this particular task is:

    public static void WriteToConsole(string message)
    {
        AttachConsole(-1);
        System.Console.WriteLine(message);
        SendKeys.SendWait("{ENTER}");
        FreeConsole();
    }

    [DllImport("Kernel32.dll")]
    private static extern bool AttachConsole(int processId);

    [DllImport("kernel32.dll")]
    private static extern bool FreeConsole();

All credits goes to Ray Burns & Scott Marlowe.

KUTlime
  • 5,889
  • 1
  • 18
  • 29
-1

Set the project type to "Console Application" instead of "Windows Application". This will cause the Application to attach to the console from which it was launched (or create a console if there was not one already).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 3
    Is there a way to do this conditionally? like if the process was run from command line it attaches to command line but otherwise it will not? – ben Jun 16 '10 at 23:27
  • Ben, I've never heard of such a thing but that would be a cool trick. I guess you might be able to detect the parent process, which would be the console window. – Jonathan Allen Jun 16 '10 at 23:33
  • 1
    @ben: There isn't an easy way - however, Jonathan's approach would most likely work. You'd need to call AttachConsole with the calling console's process ID. See: http://msdn.microsoft.com/en-us/library/ms681952(VS.85).aspx – Reed Copsey Jun 16 '10 at 23:36
  • Well I know if I access certain executables from the command line and pass in "help" or "?" as an argument it will then display its help information and do its thing. I'd like to do the same thing essentially without having to create a new command line window. I'll try doing some research into finding parent processes – ben Jun 16 '10 at 23:37
  • Thanks reed! that looks suitable to the problem with the exception that it looks like from the community content there are compatibility issues with win2000 which is an issue for me because I need to support a wide spectrum of platforms. All the same, thank you. Now at least I know my options :-) – ben Jun 16 '10 at 23:40
  • 2
    @ben: You're going to have other issues, then - WPF isn't supported on Windows 2000 either - http://msdn.microsoft.com/en-us/library/8z6watww.aspx – Reed Copsey Jun 16 '10 at 23:48
  • 1
    oh interesting alright then, maybe that really is the solution – ben Jun 16 '10 at 23:51