views:

240

answers:

3

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.

A: 

That should work just fine, double and triple check what you have done and if possible post your code. You could have a look in the project properties and check the output type is set to 'Console Application' (I believe its called that anyway :oP)

Chief17
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.
ben
ben all these comments contain pertinent info to the question, please could you add this information to the question. Thanks.
Preet Sangha
I've updated the question to reflect the additional info. Sorry about that.
ben
A: 

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
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
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
@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
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
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
@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
oh interesting alright then, maybe that really is the solution
ben
+1  A: 

This is actually trivial:

public void WriteToConsole(string message)
{
  AttachToConsole(-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);
Ray Burns