Process.Start, cmd.exe, multiple commands and echo
I am trying to open a visible Process, start cmd.exe, and then pass it arguments one at a time. In pseudo-code, this would like something like this:
1. Open "cmd.exe"
2. Send it "D:"
3. See the "D:" command echoed in the cmd.exe process window.
4. See the drive identifier change in the cmd.exe process window.
5. Send it "cd\temp"
6. See the change directory command echoed in the cmd.exe window.
7. See the cmd.exe window change the folder.
This should open a cmd.exe window that looks something like this (when done):
C:\>D: <----Sent in step 2 above.
D:\>cd\temp <----Sent in step 5 above.
d:\Temp>
Important things to note are:
1. The cmd.exe window needs to be visible.
2. The user wants to see each command echoed in the cmd.exe process window as it is sent to the process object.
3. I cannot run a batch file containing the commands. Each command must be sent individually to the cmd.exe window for procesing.
4, SendKeys cannot be used because the cmd.exe window may not always have focus.
This seems like it ought to be very simple to do yet I have been going around in circles with Process.Start and cannot seem to get it to provide all of the functionality needed above.
1. I can get it to open the Process window containing cmd.exe and even keep it open with the "/K" parm, but I have to pass it multiple commands concatenated with " && " statement. This appears to run all the commands at before returning any results so I cannot see how each command executed in the window. IOW, if I send it "D: && cd\Temp" as shown above, all I get is the command prompt now showing "D:\Temp". Neither "D:" nor "cd\temp" is ever echoed to the cmd.eexe process window, just the end result, and the end user needs to see the result of each step as it is processed.
2. I've tried sending subsequent commands using another Process.Start, like this:
Process.Start("cmd.exe, "\K d:")
Process.Start("cmd.exe, "\K cd\temp")
but that opens another cmd.exe process window. "cd\temp" won't work unless d: is executed first in the same Process window.
3. I've tried redirecting the Standard Input stream and the cmd.exe Process window opens completely black; I get no DOS prompt or anything to even indicate the window is running cmd.exe. Nothing sent to it using Process.StandardInput.WriteLine ever appears in the window and I cannot tell if it has done anything at all. RedirectStandardInput appears to be causing this, because when I comment this line out, the command window opens as expected. Possibly further complicating things are sites I've found which indicate the cmd.exe requires redirecting input AND output, which may be why the Process window is empty. But If I try to read the redirected output, it hangs forever the process.stereamreader.ReadToEnd, which leads me to believe it has not returned from performing the command sent.
I'd really appreciate a code snippet that references cmd.exe, sends two non-concatenated commands to cmd.exe (IOW, not concatenated in the arguments using "&&", and echoes both the command sent and results returned. Most on-line code samples I've Googled run something other than cmd.exe and those referencing cmd.exe only seem to work when passing concatenated commands, which does not produce teh results needed. Thanks!
|