process - Redirecting command line output, including text prompts for user input C# -
many days i'm googling question, did not found answers. have batch file this.
@echo off echo sample output. set /p uname=please enter name: if "%uname%"=="" goto error echo hello %uname%, welcome dos inputs! goto end :error echo did not enter name! bye bye!! :end pause
how can see command, print "this sample output." line , ask user name. on c# "this sample output." line received output data, text "please enter name: " receive after inputting thing in console. so, how can receive prompt output before inputting something, can redirect full console? (if can so). below test application.
using system; using system.diagnostics; namespace commandexecuter { static class program { static void main() { try { var start = new processstartinfo { filename = @"c:\run.cmd", useshellexecute = false, redirectstandardoutput = true, redirectstandarderror = true }; using (var process = new process()) { process.startinfo = start; process.outputdatareceived += process_outputdatareceived; process.errordatareceived += process_errordatareceived; process.start(); process.beginoutputreadline(); process.beginerrorreadline(); process.waitforexit(); process.canceloutputread(); process.cancelerrorread(); } } catch (exception ex) { console.writeline("failed execute command: " + ex.message); } { console.readline(); } } private static void process_outputdatareceived(object sender, datareceivedeventargs e) { if (!string.isnullorempty(e.data)) { console.write("outputdatareceived: " + e.data + environment.newline); } } private static void process_errordatareceived(object sender, datareceivedeventargs e) { if (!string.isnullorempty(e.data)) { console.write("errordatareceived: " + e.data + environment.newline); } } } }
change if while. think getting multiple return lines
private static void process_outputdatareceived(object sender, datareceivedeventargs e) { while (!string.isnullorempty(e.data)) { console.write("outputdatareceived: " + e.data + environment.newline); } }
Comments
Post a Comment