471,594 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

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
6 35991
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
RBGames
1 Bit
Was googling how to do this and found this post. Using this info and some other posts I put this together. Maybe someone else will land here and find this useful. BTW, this is C# 10.0.

Expand|Select|Wrap|Line Numbers
  1. public static class ConsoleEx
  2. {
  3.     private static TextWriter? defaultConsoleOut = null;
  4.  
  5.     public static void SetEcho(bool flag)
  6.     {
  7.         if (defaultConsoleOut == null) defaultConsoleOut = Console.Out;
  8.  
  9.         if (flag)
  10.         {
  11.             Console.SetOut(defaultConsoleOut);
  12.         }
  13.         else
  14.         {
  15.             Console.SetOut(TextWriter.Null);
  16.         }
  17.     }
  18. }
  19.  
Usage would be:
Expand|Select|Wrap|Line Numbers
  1. // turn console output off
  2. ConsoleEx.SetEcho(false);
  3.  
  4. // turn console output on
  5. ConsoleEx.SetEcho(true);
  6.  
Jan 6 '22 #7

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
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.