public static string ExecPython(string path, string args, ExecCmdWindowOption execCmdOption, ExecCmdOutputOption outputOption, ProcessAccountInfo processAccountInfo) { try { if (!FileHelper.IsFileExisting(PythonExecPath)) { throw new ArgumentException("Python exec file does not exist!"); } if (!System.IO.File.Exists(path)) { throw new ArgumentException("File: '" + path + "' does not exist!"); } var inputStr = PythonExecPath + " " + path + " " + args; return(ExecCmd(inputStr, execCmdOption, outputOption, processAccountInfo)); } catch (Exception ex) { throw new ArgumentException("\n>> " + TypeName + ".RunPython Error: " + ex.Message); } }
public static string ExecCmd(string inputStr, ExecCmdWindowOption execCmdOption, ExecCmdOutputOption outputOption, ProcessAccountInfo processAccountInfo) { if (string.IsNullOrEmpty(inputStr)) { return(""); } var process = new Process(); process.StartInfo.FileName = "cmd.exe"; //process.StartInfo.Arguments = args; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//does not work, the windows shown or hidden depends on "process.StartInfo.CreateNoWindow" var execCmdMode = ExecCmdMode.AsyncWindow; if (execCmdOption == ExecCmdWindowOption.ShowWindow) { execCmdMode = ExecCmdMode.AsyncWindow; } else if (execCmdOption == ExecCmdWindowOption.NoWindow & outputOption == ExecCmdOutputOption.None) { execCmdMode = ExecCmdMode.NoWindow; } else if (execCmdOption == ExecCmdWindowOption.NoWindow & outputOption != ExecCmdOutputOption.None) { execCmdMode = ExecCmdMode.NoWindowWithOutput; } else if (execCmdOption == ExecCmdWindowOption.BlankWindow & outputOption == ExecCmdOutputOption.None) { execCmdMode = ExecCmdMode.BlankWindow; } else if (execCmdOption == ExecCmdWindowOption.BlankWindow & outputOption != ExecCmdOutputOption.None) { execCmdMode = ExecCmdMode.BlankWindowWithOutput; } var returnOutput = false; if (execCmdMode == ExecCmdMode.AsyncWindow) { inputStr = "start " + _startBatPath + " " + inputStr; process.StartInfo.CreateNoWindow = true; returnOutput = false; } else if (execCmdMode == ExecCmdMode.SyncWindow) { inputStr = "start " + _startBatPath + " " + inputStr; process.StartInfo.CreateNoWindow = true; returnOutput = true; } else if (execCmdMode == ExecCmdMode.NoWindow) { process.StartInfo.CreateNoWindow = true; returnOutput = false; } else if (execCmdMode == ExecCmdMode.NoWindowWithOutput) { process.StartInfo.CreateNoWindow = true; returnOutput = true; } else if (execCmdMode == ExecCmdMode.BlankWindow) { process.StartInfo.CreateNoWindow = false; //when CreateNoWindow = false, popup a empty cmd window, no any exec result appears. returnOutput = false; } else if (execCmdMode == ExecCmdMode.BlankWindowWithOutput) { process.StartInfo.CreateNoWindow = false; returnOutput = true; } if (processAccountInfo != null) { process.StartInfo.Domain = processAccountInfo.Domain; process.StartInfo.UserName = processAccountInfo.UserName; var pw = new System.Security.SecureString(); foreach (var v in processAccountInfo.Password.ToCharArray()) { pw.AppendChar(v); } process.StartInfo.Password = pw; } try { process.Start(); //process.StandardInput.WriteLine("cd " + @"C:\windows\system32"); process.StandardInput.WriteLine(inputStr); process.StandardInput.WriteLine("exit"); var outputStr = ""; if (returnOutput) { outputStr = process.StandardOutput.ReadToEnd();//When use 'start' to start a real new window, the Output is on the new window, not this empty window. so if you want a return output , don't use 'start' } process.WaitForExit(); process.Close(); return(outputStr); } catch (Exception ex) { throw new ArgumentException("\n>> " + TypeName + ".ExecCmd Error: " + ex.Message); } }