c# - Execute couple command in cmd. Executed only one command -


hellp. in 'plumbing' have 3 command should performed sequentially, , each request must wait until end of previous command. have done 1st request, 2nd , 3rd skips... please suggest how change 'plumbing'?

string strcmdtext = s1; var startinfo = new processstartinfo    {    filename = "cmd.exe",    redirectstandardinput = true,    redirectstandardoutput = true,    useshellexecute = false,    createnowindow = true    };  var process = new process { startinfo = startinfo }; process.start(); process.standardinput.writeline(strcmdtext); process.waitforexit();  string strcmdtext1 = s2; process.standardinput.writeline(strcmdtext1); process.waitforexit();  string strcmdtext2 = s3; process.standardinput.writeline(strcmdtext2); process.standardinput.writeline("exit"); 

thank you.

let's go through code:

  • you start instance of cmd.exe:

    var process = new process { startinfo = startinfo }; process.start(); 
  • you write command standard input:

    process.standardinput.writeline(strcmdtext); 
  • and wait cmd.exe exit:

    process.waitforexit(); 
  • now, write command standard input:

    string strcmdtext1 = s2; process.standardinput.writeline(strcmdtext1); 
  • wait, what? cmd.exe exited in previous step, there's no more process send command in first place.

  • then wait process exit, it's dead long time ago:

    process.waitforexit(); 
  • and repeat same non-working code:

    string strcmdtext2 = s3; process.standardinput.writeline(strcmdtext2); process.standardinput.writeline("exit"); 

you should better understand what's the problem now. looks cmd.exe quits after executing first command.

there's couple things can try:

  • get rid of cmd.exe altogether. unless execute batch script can directly call intended executable (like python.exe).

  • start 3 different instances of cmd.exe 3 commands.

  • try pass arguments cmd.exe, /q.

try first approach first, it's cleanest one.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -