Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't actually necessarily need the steps to be run in the same process, but just sequentially, you can use the 'WaitForExit' method on Process, which will block execution on the current thread until the process completes. For example, the following code will open notepad, wait for you to close it (process exits) and then open a second instance of notepad:</p> <pre><code> p.StartInfo.FileName = "notepad.exe"; p.Start(); p.WaitForExit(); p.Close(); p.StartInfo.FileName = "notepad.exe"; p.Start(); p.WaitForExit(); p.Close(); </code></pre> <p>However, the two instances will occur in different processes - you can see this by checking the PID of notepad.exe in Task Manager, for example.</p> <p>If you really want to run several CLI programs in sequence in the same process, you may want to simply open a process running 'cmd.exe' and feed commands to it via its StandardInput. The StandardInput stream is the stream that the program reads its input from - in the case of cmd.exe, it's exactly as if you'd typed those commands into it directly. For example, try this:</p> <pre><code> Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.Start(); p.StandardInput.WriteLine("echo Hello there!"); p.StandardInput.WriteLine("dir"); p.StandardInput.WriteLine("echo Goodbye!"); p.WaitForExit(); p.Close(); </code></pre> <p>One problem with this approach is that you don't know whether the last command you passed in has actually finished executing that easily... the 'WaitForExit' call will wait until the process exits of its own accord, but in the case of the cmd.exe window that would require a user to come along and click the 'X' in the corner. You could skip the 'WaitForExit' call, but that may terminate the process while your last command is still actually doing something.</p> <p>Luckily in this particular case, cmd.exe will terminate itself if you feed it the 'exit' command:</p> <pre><code> Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.Start(); p.StandardInput.WriteLine("echo Hello there!"); p.StandardInput.WriteLine("dir"); p.StandardInput.WriteLine("echo Goodbye!"); p.StandardInput.WriteLine("exit"); p.WaitForExit(); p.Close(); </code></pre> <p>As it goes, you have to set the UseShellExecute and RedirectStandardInput properties to true and false respectively, or using the StandardInput stream will throw an exception of one type or another - try it and see! You can read more about it here:</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload