c# - Redirect console.writeline from windows application to a string -


i have external dll written in c# , studied assemblies documentation writes debug messages console using console.writeline.

this dll writes console during interaction ui of application, don't make dll calls directly, capture console output , think got intialize in form load , captured text later.

i redirect output string variable.

i tried console.setout, use redirect string not easy.

as seems want catch console output in realtime, figured out might create own textwriter implementation fires event whenever write or writeline happens on console.

the writer looks this:

    public class consolewritereventargs : eventargs     {         public string value { get; private set; }         public consolewritereventargs(string value)         {             value = value;         }     }      public class consolewriter : textwriter     {         public override encoding encoding { { return encoding.utf8; } }          public override void write(string value)         {             if (writeevent != null) writeevent(this, new consolewritereventargs(value));             base.write(value);         }          public override void writeline(string value)         {             if (writelineevent != null) writelineevent(this, new consolewritereventargs(value));             base.writeline(value);         }          public event eventhandler<consolewritereventargs> writeevent;         public event eventhandler<consolewritereventargs> writelineevent;     } 

if it's winform app, can setup writer , consume events in program.cs this:

    /// <summary>     /// main entry point application.     /// </summary>     [stathread]     static void main()     {         using (var consolewriter = new consolewriter())         {             consolewriter.writeevent += consolewriter_writeevent;             consolewriter.writelineevent += consolewriter_writelineevent;              console.setout(consolewriter);             application.enablevisualstyles();             application.setcompatibletextrenderingdefault(false);             application.run(new form1());         }     }      static void consolewriter_writelineevent(object sender, program.consolewritereventargs e)     {         messagebox.show(e.value, "writeline");     }      static void consolewriter_writeevent(object sender, program.consolewritereventargs e)     {         messagebox.show(e.value, "write");     } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -