469,282 Members | 1,892 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

C#-ConsoleApp: How to turn off console notifications.

7
Hello
I developing console application that sometimes writes some information to console using Console.WriteLn or Console.Write.
Is it possible to turn on/off displaying of messages that will be written out into console?

Sometimes I don't want anything to be written in the console and sometimes I need it. I hope someone knows solution to this.

thanks
AkipNG
Jun 1 '07 #1
5 6278
Plater
7,872 Expert 4TB
comment out the WriteLn() and Write() calls?

or

put them in a wrapper function that has logic to know if it should or should not write to the console:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void WriteLine(string msg)
  3. {
  4.    if (ShouldWrite)
  5.    {
  6.       Console.WriteLn(msg);
  7.    }
  8. }
  9.  
Jun 1 '07 #2
akipng
7
comment out the WriteLn() and Write() calls?

or

put them in a wrapper function that has logic to know if it should or should not write to the console:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void WriteLine(string msg)
  3. {
  4.    if (ShouldWrite)
  5.    {
  6.       Console.WriteLn(msg);
  7.    }
  8. }
  9.  

Thank you for input.
This is not solution I was looking for. Some of the dll's I use generates console output and I can't use wrapper method on in that libraries.

Is there an other way to turn off console? Something like ECHO OFF in old dos scripts?

thanks
AkipNG
Jun 1 '07 #3
Plater
7,872 Expert 4TB
You can redirec the ouput stream to elsewhere with Console.SetOut() (and also .SetIn() and SetError())
Give a quick check in MSDN on them, should accomplish what you're looking for.
Jun 1 '07 #4
akipng
7
You can redirec the ouput stream to elsewhere with Console.SetOut() (and also .SetIn() and SetError())
Give a quick check in MSDN on them, should accomplish what you're looking for.
Great!
That worked out. I saved current console 'Console.Out' in 'saveOut' variable and when I need to call a Console command I switch it and post it then switch back to TextWriter.Null. So no other text is spamming console.

Expand|Select|Wrap|Line Numbers
  1. private TextWriter saveOut=null;
  2. public void WriteToConsole(messageToConsole)
  3. {
  4. if (saveOut==null) // first time use
  5.                 saveOut = Console.Out;
  6.             Console.SetOut(saveOut); 
  7.             Console.WriteLine(messageToConsole);
  8.             Console.SetOut(TextWriter.Null);
  9. }
  10.  
Thank you :)
AkipNG
Jun 1 '07 #5
akipng
7
The solution provided above only works when application uses Console static object to write messages.
Unfortunately I use library (dll) that still writes messages to console, I believe that library is written in c++ and uses system calls to do that.
Anyway to stop it? maybe intercept the message in WndProc that cause the console message placement, how?

thanks
AkipNG
Jun 1 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by djc | last post: by
10 posts views Thread by Stephany Young | last post: by
3 posts views Thread by jim | last post: by
15 posts views Thread by Michael C | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.