示例#1
0
        public virtual HostCommand CreateCommand(string exe, string args)
        {
            var command = new HostCommand(exe, args);

            command.CurrentDirectory = baseFolder;
            return(command);
        }
示例#2
0
        static void Main(string[] args)
        {
            CommandExecutor pshell = new CommandExecutor(Environment.CurrentDirectory);

            while (true)
            {
                Console.Write(":>");
                string line = Console.ReadLine();

                if (line.ToLower() == "exit")
                {
                    break;
                }
                else
                {
                    string [] arguments = line.Split(' ');
                    if (arguments.Length > 1)
                    {
                        string      target     = arguments[0];
                        string      restOfLine = line.Substring(target.Length).Trim();
                        HostCommand command    = pshell.CreateCommand(target, restOfLine);
                        var         output     = pshell.ExecuteCommand(command);
                        Console.WriteLine($"Status: {output.ExecuteStatus} Time Started: {output.Started} Time Ended: {output.Ended}");
                        if (output.ExecuteStatus != CommandStatus.Error)
                        {
                            Console.WriteLine(output.ConsoleOutput);
                        }
                        else
                        {
                            Console.WriteLine($"Some Error : { output.ErrorOutput } ");
                        }
                    }
                }
            }
        }
示例#3
0
        public virtual HostCommand ExecuteCommand(HostCommand command)
        {
            try
            {
                command.Started       = DateTime.Now;
                command.ExecuteStatus = CommandStatus.Started;

                Process process = new Process();
                process.StartInfo = new ProcessStartInfo(command.Target, command.Parameters);
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.RedirectStandardError  = true;
                process.StartInfo.WorkingDirectory       = command.CurrentDirectory;

                process.EnableRaisingEvents = true;
                process.Exited += new EventHandler((s1, e1) => {
                    command.ExecuteStatus = CommandStatus.Exited;
                });
                //* Set your output and error (asynchronous) handlers
                process.OutputDataReceived += new DataReceivedEventHandler((s2, e2) => {
                    command.ConsoleOutput += Environment.NewLine + e2.Data;
                });
                process.ErrorDataReceived += new DataReceivedEventHandler((s3, e3) => {
                    command.ErrorOutput  += Environment.NewLine + e3.Data;
                    command.ExecuteStatus = CommandStatus.Error;
                });
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
            }
            catch (Exception ex)
            {
                command.ErrorOutput += ex.Message;

                command.ExecuteStatus = CommandStatus.Error;
            }
            finally
            {
                command.Ended = DateTime.Now;
            }
            return(command);
        }