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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akipng
    New Member
    • Jun 2007
    • 7

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

    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
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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:

    Code:
    public void WriteLine(string msg)
    {
       if (ShouldWrite)
       {
          Console.WriteLn(msg);
       }
    }

    Comment

    • akipng
      New Member
      • Jun 2007
      • 7

      #3
      Originally posted by Plater
      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:

      Code:
      public void WriteLine(string msg)
      {
         if (ShouldWrite)
         {
            Console.WriteLn(msg);
         }
      }

      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

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        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.

        Comment

        • akipng
          New Member
          • Jun 2007
          • 7

          #5
          Originally posted by Plater
          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.

          Code:
          private TextWriter saveOut=null;
          public void WriteToConsole(messageToConsole)
          {
          if (saveOut==null) // first time use
                          saveOut = Console.Out;
                      Console.SetOut(saveOut); 
                      Console.WriteLine(messageToConsole);
                      Console.SetOut(TextWriter.Null);
          }
          Thank you :)
          AkipNG

          Comment

          • akipng
            New Member
            • Jun 2007
            • 7

            #6
            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

            Comment

            • RBGames
              New Member
              • Jan 2022
              • 1

              #7
              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.

              Code:
              public static class ConsoleEx
              {
                  private static TextWriter? defaultConsoleOut = null;
              
                  public static void SetEcho(bool flag)
                  {
                      if (defaultConsoleOut == null) defaultConsoleOut = Console.Out;
              
                      if (flag)
                      {
                          Console.SetOut(defaultConsoleOut);
                      }
                      else
                      {
                          Console.SetOut(TextWriter.Null);
                      }
                  }
              }
              Usage would be:
              Code:
              // turn console output off
              ConsoleEx.SetEcho(false);
              
              // turn console output on
              ConsoleEx.SetEcho(true);

              Comment

              Working...