public static string Shell(string line, bool suppresserrors) { System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe", @"/C " + line); psi.CreateNoWindow = true; //psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; psi.UseShellExecute = false; psi.RedirectStandardOutput = true; if (!suppresserrors) { psi.RedirectStandardError = true; } string result; using (System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi)) { ShellErrInfo sei = null; System.Threading.Thread errthd = null; if (!suppresserrors) { sei = new ShellErrInfo(); sei.reader = process.StandardError; errthd = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(shellerrthreadproc)); errthd.Start(sei); } result = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (!suppresserrors) { errthd.Join(); if (!string.IsNullOrEmpty(sei.err)) { sei.err = sei.err.Trim(); if (sei.err.Length != 0) { throw new ShellException(line, sei.err); } } } } return(result); }
public static string Shell(string line, bool suppresserrors) { System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe", @"/C " + line); psi.CreateNoWindow = true; //psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; psi.UseShellExecute = false; psi.RedirectStandardOutput = true; if (!suppresserrors) { psi.RedirectStandardError = true; } string result; using (System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi)) { ShellErrInfo sei = null; System.Threading.Thread errthd = null; if (!suppresserrors) { sei = new ShellErrInfo(); sei.reader = process.StandardError; errthd = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(shellerrthreadproc)); errthd.Start(sei); } result = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (!suppresserrors) { errthd.Join(); if (!string.IsNullOrEmpty(sei.err)) { sei.err = sei.err.Trim(); if (sei.err.Length != 0) { throw new ShellException(line, sei.err); } } } } return result; }
static void shellerrthreadproc(object obj) { ShellErrInfo sei = (ShellErrInfo)obj; sei.err = sei.reader.ReadToEnd(); }