C# PROGRAMMING

Redirecting The Command-line To A Text Block

Article by:
Date Published:
Last Modified:

It involves inheriting from the StringWriter class, and overwriting it’s WriteLine function with your own code that writes the text to the text block (or any other text-capable object), rather than Windows writing it to the system console (whatever that may be).

Note that this code is suitable for passing into functions that require a System.IO.Stream, but this does not redirect messages that are being sent directly to the command line.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace StringRedirect
{    
    /// <summary>
    /// Allows text blocks to appear like a command line for serial out string messages.
    /// </summary>
    public class TextBlockStreamWriter : StringWriter
    {
        private TextBlock _textBlock;

        public string Text
        {
            get 
            { 
                return _textBlock.Text; 
            }
            set
            {
                _textBlock.Text += value;
            }
        }

        public TextBlockStreamWriter(ref TextBlock t)
        {
            _textBlock = t;
        }

        public override void WriteLine(string msg)
        {
            Text = msg + "\n";
        }
    }
}

Authors

Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Tags

    comments powered by Disqus