예제 #1
0
        /// =======================================================================================================
        // String based command line executer. Return all the standard output text. Ignore error text, as this isn't considered important (this is not a debug application).
        public static Status RunCmd(AutoReport auto_report,
                                    Map map,
                                    string fileName       = "cmd.exe",
                                    string path           = @"C:\",
                                    string command        = "",
                                    int timeout           = 120,
                                    string commandPrefix  = "/C",
                                    bool use_shell        = false,
                                    bool redirect_std_out = true,
                                    bool redirect_std_in  = false,
                                    bool redirect_std_err = false,
                                    bool nowindow         = true)
        {
            DateTime starttime = DateTime.Now;

            if (!CAT.AbortAll)
            {
                try { if (string.IsNullOrWhiteSpace(path) && Directory.Exists(command))
                      {
                          command = @"%SystemRoot%\explorer.exe " + command; auto_report.AddOverlayedMessage(map, "Applied assumption: " + command, Status.WARN);
                      }
                } catch { }
                path = DISK.AutoAddBaseDirectory(path);
                if (!Directory.Exists(path))
                {
                    if (auto_report != null)
                    {
                        auto_report.AddOverlayedMessage(map, "Path '" + path + "' is invalid", Status.FAIL);
                    }
                }
                Process process = new Process();
                try
                {
                    ProcessStartInfo processinfo = new ProcessStartInfo()
                    {
                        CreateNoWindow         = nowindow,
                        UseShellExecute        = use_shell,
                        RedirectStandardOutput = redirect_std_out,
                        RedirectStandardError  = redirect_std_err,
                        RedirectStandardInput  = redirect_std_in,
                        WorkingDirectory       = path,
                        WindowStyle            = ProcessWindowStyle.Normal,
                        FileName  = fileName,
                        Arguments = commandPrefix + command
                    };
                    string alias = path + ">" + command;
                    process.StartInfo = processinfo;
                    process.Start();
                    string standardoutputtext = "";
                    string overlayname        = "standardOutput" + map.A + map.B + map.C;
                    if (auto_report != null)
                    {
                        auto_report.AddOverlay(overlayname, "", Status.INFO);
                    }
                    while (!process.StandardOutput.EndOfStream)
                    {
                        string line = process.StandardOutput.ReadLine();
                        standardoutputtext += line + "\r\n";
                        if (auto_report != null)
                        {
                            auto_report.UpdateOverlay(overlayname, standardoutputtext, Status.INFO); auto_report.UpdateCommandProgress(map);
                        }
                        if (CAT.AbortAll || CAT.AbortCurrent)
                        {
                            CAT.AbortCurrent = false;
                            process.Close();
                            if (auto_report != null)
                            {
                                auto_report.AddOverlayedMessage(map, "User aborted after " + (DateTime.Now - starttime).TotalSeconds + "s", Status.ABORT);
                            }
                            return(Status.ABORT);
                        }
                        Thread.Sleep(10);
                        if ((DateTime.Now - starttime).TotalMilliseconds > (timeout * 1000))
                        {
                            process.Close();
                            if (auto_report != null)
                            {
                                auto_report.AddOverlayedMessage(map, "Timeout occured (timeout = " + timeout + "s)", Status.TIMEOUT);
                            }
                            return(Status.TIMEOUT);
                        }
                    }
                    if (auto_report != null)
                    {
                        auto_report.AddMessage(map, standardoutputtext, Status.INFO);
                    }
                    process.Close();
                    return(Status.PASS);
                }
                catch (Exception e)
                {
                    try { process.Close(); } catch { }
                    if (auto_report != null)
                    {
                        auto_report.AddOverlayedMessage(map, ErrorMsg(e), Status.FAIL);
                    }
                }
            }
            else
            {
                return(Status.ABORT);
            }
            return(Status.PASS);
        }