c# - Listen for cmd output and log to file -
i'm trying make c# program can listen , output cmd.exe , log file. example, if run exe , runs command in cmd echo "hello"
, want echo "hello"
written in file.
i know need use filesystem
, process
maybe?
if possible, , appreciated. thanks.
here's quick little example should work. there lot of examples out there, i'll try looking on stackoverflow , post 1 well...
string cmd_to_run = "dir"; // whatever you'd be... // set our initial parameters out process processstartinfo p_info = new processstartinfo(); p_info.filename = "cmd"; p_info.arguments = "/c " + cmd_to_run; p_info.useshellexecute = false; // instantiate new process process p_to_run = new process(); p_to_run.startinfo = p_info; // wait exit (i chose 120 seconds) // waiting output here not asynchronous, depending on task may want p_to_run.start(); p_to_run.waitforexit(120 * 1000); string output = p_to_run.standardoutput.readtoend(); // here our output
here's process class msdn overview (there's quick example on page): https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
and here's example dealing calling readtoend() on process: standardoutput.readtoend() hangs
Comments
Post a Comment