Executing BatchFile from C# Program -
i writing batch file , executing through c# program.
writing batch file :
i path, executable name , arguments app.config , write them batch file.
executing batch file :
once write batch file pass file name below function executes batch file launches application.
problem :
my program write lot of batch files executed after each , every file written. find that, times applications not started means batch files not executed. dint error messages or prompts failure of batch file execution.
expected solution :
any problem in executing batch file, should able log or prompt error.
code executes batch file :
system.diagnostics.processstartinfo procinfo = new system.diagnostics.processstartinfo("cmd.exe"); procinfo.useshellexecute = false; procinfo.redirectstandarderror = true; procinfo.redirectstandardinput = true; procinfo.redirectstandardoutput = true; system.diagnostics.process process = system.diagnostics.process.start(procinfo); system.io.streamreader stream = system.io.file.opentext(batchpath + latestfilename); system.io.streamreader sroutput = process.standardoutput; system.io.streamwriter srinput = process.standardinput; while (stream.peek() != -1) { srinput.writeline(stream.readline()); } log.flow_writetologfile("executed .bat file : " + latestfilename); stream.close(); process.close(); srinput.close(); sroutput.close(); very urgent..help !!!
i'm not sure problem lies i've had no problems following code:
using (filestream file = new filestream("xyz.cmd", filemode.create)) { using (streamwriter sw = new streamwriter(file)) { sw.write("@echo ====================\n"); sw.close(); } } process p = new process(); p.startinfo.useshellexecute = false; p.startinfo.filename = "xyz.cmd"; //p.startinfo.redirectstandardoutput = true; p.start(); //string s = p.standardoutput.readline(); //while (s != null) { // messagebox.show(s); // s = p.standardoutput.readline(); //} p.waitforexit(); obviously that's been cut down bit purposes of hiding "secret sauce" that's code being used in production without issues.
i have 1 question. why don't execute cmd file directly rather running cmd.exe?
probably first thing i'd print out batchpath + latestfilename value see if you're creating weirdly named files prevent cmd.exe running them.
Comments
Post a Comment