标签云

微信群

扫码加入我们

WeChat QR Code

We have a few commands(batch files/executables) on our network path which we have to call to initialize our 'development environment' for that command window. It sets some environmental variables, adds stuff to the Path etc. (Then only whatever working commands we type will be recognized & I don't know what goes inside those initializing commands)Now my problem is, I want to call a series of those 'working commands' using a C# program, and certainly, they will work only if the initial setup is done. How can I do that? Currently, I'm creating a batch file by scratch from the program like this for example: file.Writeline("InitializationStep1.bat")file.Writeline("InitializeStep2.exe")file.Writeline("InitializeStep3.exe")Then the actual commandsfile.Writeline("Dowork -arguments -flags -blah -blah")file.Writeline("DoMoreWork -arguments -flags -blah -blah")Then finally close the file writer, and run this batch file. Now if I directly execute this using Process.<strike>Run</strike>Start("cmd.exe","Dowork -arguments"); it won't run.How can I achieve this in a cleaner way, so that I have to run the initialization commands only once? (I could run cmd.exe each time with all three initializers, but they take a lot of time so I want to do it only once)


Hi friends, sorry for the confusion, I meant Process.Start only and not Process.Run. Also, I guess I was not clear enough in explaining my problem. I know how to execute these kind of basic commands using Process.Start and ProcessStartInfo as well. My problem is, once I execute it, it's all over - while I want to call the 'initialize' batch file with that process, and THEN the actual working commands in the SAME process as only that one has correct environment variables and proper Path set.

2019年09月18日59分08秒

also, by "it won't run" I meant it won't run because the prerequisites for running the command are set in the command prompt only by calling those initializer batch and exe files. So this command Dowork is not even recognized if I run directly with process.

2019年09月18日59分08秒

Is this the control flow: 1. Create batch file (without arguments), 2. Execute commands in the batch file with a specified arguments in another process.? If I am incorrect, then what is the exact control flow? You above comments are not very precise.

2019年09月19日59分08秒

Hi Devendra, as also edited in the question, Process.Run was a typo, it was Process.Start. My problem is not running just one command or batch file. It is how to run a series of commands, in the same cmd window created once. Do you know how to do that?

2019年09月18日59分08秒

Piyush, you can programmatically create a batch file with each command on a separate line. I have edited the answer to add an example.

2019年09月19日59分08秒

Hi Devendra, that is what I was originally doing(which was working as well), but I wanted a cleaner approach of executing a series of steps in the same command window process.

2019年09月18日59分08秒

Piyush, batch files are the way to execute a series of commands. But you could put all the commands in a single batch file requiring a single call initializer commands. An alternative would be to communicate with the child process (cmd.exe) using IPC, but this would complicate the existing solution. Nevertheless, you can have a look at: stackoverflow.com/questions/50153/… and a similar question at: stackoverflow.com/questions/1515965/…

2019年09月19日59分08秒

As far as I know, IPC is useful only when both of the programs communicating are written by us (or we know about the code of the other one). I don't know how to communicate with cmd.exe. And the second link is not at all related to my question. The essence of my original question was actually very short :"How to send a sequence of commands to a SINGLE command prompt process, without using a batch file, which I was anyway doing."

2019年09月18日59分08秒

Hi Hakeem, as I also edited in the question, I was indeed talking about Process.Run and this doesn't solve my problem.

2019年09月18日59分08秒

Aah ! Type once again. I was indeed talking about 'Process.Start' and this doesn't solve my problem :(

2019年09月18日59分08秒