Is it possible to log message to cmd.exe in C#/.Net? -


is possible log message winforms app cmd.exe process started programmatically? i've tried kinds of variations of following code:

private void button1_click(object sender, eventargs e) {     log("button has been pressed"); }  private void log(string message) {     var process = process.start(new processstartinfo("cmd.exe", @"/k ""more""")     {         useshellexecute = false,         redirectstandardinput = true,     });     process.standardinput.writeline(message); } 

unfortunately, console window blinks half of second , that's it.

please don't answer want do, because right i'm curious if it's possible :)

you're in luck. solved code generator toy project had floating around here.

using attachconsole or allocconsole win32 api's can achieve result, using system.console.writeline (or read, or error.writeline etc) would.

the following snippet figures out when forms app started console window, , attaches it's parent console; if doesn't work, create it's own console, , remember keep open (so doesn't vanish before user can read output, example).

    [dllimport("kernel32.dll")] static extern bool allocconsole();     [dllimport("kernel32.dll")] static extern bool attachconsole(int pw);     private static bool _hasconsole, _keepconsoleopen;     static private void ensureconsole()     {         _hasconsole      =  _hasconsole || attachconsole(-1);         _keepconsoleopen = !_hasconsole || _keepconsoleopen;         _hasconsole      =  _hasconsole || allocconsole();     }      [stathread]     private static void main(string[] args)     {           ensureconsole();            // usual stuff -- program            if (_keepconsoleopen)           {               console.writeline("(press enter)...");               console.read();           }     } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -