public String run_os_cmd(string command, string args, String Separator) { //String Separator = "|"; CmdResponse myresult = new CmdResponse(); ProcessStartInfo start = new ProcessStartInfo(); start.FileName = command; start.Arguments = string.Format("\"{0}\"", args); start.UseShellExecute = false; // Do not use OS shell start.CreateNoWindow = true; // We don't need new window start.RedirectStandardOutput = true; // Any output, generated by application will be redirected back start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions) using (Process process = Process.Start(start)) { using (StreamReader reader = process.StandardOutput) { string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test") myresult.Result = result; if (result != "") { myresult.StdOutputPresent = true; } else { myresult.StdOutputPresent = false; } myresult.RetCode = process.ExitCode; myresult.Error = stderr; if (stderr != "") { myresult.StdErrorPresent = true; } else { myresult.StdErrorPresent = false; } return(prettyprintresponse(myresult, Separator)); } } return(prettyprintresponse(myresult, Separator)); }
public String prettyprintresponse(CmdResponse res, String Separator) { String RESP = res.RetCode + Separator + res.StdOutputPresent + Separator + res.StdErrorPresent + Separator + res.Result + Separator + res.Error; return(RESP); }