Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This should work. You could try to dump out the contents of the output and error streams in order to find out what's happening:</p> <pre><code>static void ExecuteCommand(string command) { int exitCode; ProcessStartInfo processInfo; Process process; processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); processInfo.CreateNoWindow = true; processInfo.UseShellExecute = false; // *** Redirect the output *** processInfo.RedirectStandardError = true; processInfo.RedirectStandardOutput = true; process = Process.Start(processInfo); process.WaitForExit(); // *** Read the streams *** // Warning: This approach can lead to deadlocks, see Edit #2 string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); exitCode = process.ExitCode; Console.WriteLine("output&gt;&gt;" + (String.IsNullOrEmpty(output) ? "(none)" : output)); Console.WriteLine("error&gt;&gt;" + (String.IsNullOrEmpty(error) ? "(none)" : error)); Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand"); process.Close(); } static void Main() { ExecuteCommand("echo testing"); } </code></pre> <p><strong>* EDIT *</strong> </p> <p>Given the extra information in your comment below, I was able to recreate the problem. There seems to be some security setting that results in this behaviour (haven't investigated that in detail). </p> <p>This <em>does</em> work if the batch file is not located in <code>C:\Windows\System32</code>. Try moving it to some other location, e.g. the location of your executable. Note that keeping custom batch files or executables in the Windows directory is bad practice anyway.</p> <p><strong>* EDIT 2 *</strong> It <a href="https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput%28v=vs.110%29.aspx" rel="noreferrer">turns out</a> that if the streams are read synchronously, a deadlock can occur, either by reading synchronously before <code>WaitForExit</code> or by reading both <code>stderr</code> and <code>stdout</code> synchronously one after the other. </p> <p>This should not happen if using the asynchronous read methods instead, as in the following example:</p> <pre><code>static void ExecuteCommand(string command) { var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); processInfo.CreateNoWindow = true; processInfo.UseShellExecute = false; processInfo.RedirectStandardError = true; processInfo.RedirectStandardOutput = true; var process = Process.Start(processInfo); process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =&gt; Console.WriteLine("output&gt;&gt;" + e.Data); process.BeginOutputReadLine(); process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =&gt; Console.WriteLine("error&gt;&gt;" + e.Data); process.BeginErrorReadLine(); process.WaitForExit(); Console.WriteLine("ExitCode: {0}", process.ExitCode); process.Close(); } </code></pre>
 

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