Пример #1
0
        private void Init(string command, AgentMode mode, TimeSpan interval, DateTime startTime)
        {
            if (command.IsNull())
            {
                throw new ArgumentNullException("command", "command 不能为 null");
            }

            this.Mode        = mode;
            this.Interval    = interval;
            this.StartTime   = startTime;
            this.Last        = DateTime.MaxValue;
            this.HasShutDown = false;

            ValueSet <int, int> result = command.IndexOfWhiteSpace();

            this.Process = new Process();

            if (result.IsNull())
            {
                this.Process.StartInfo.FileName = command;
            }
            else
            {
                this.Process.StartInfo.FileName  = command.Substring(0, result.Value1);
                this.Process.StartInfo.Arguments = command.Substring(result.Value1 + result.Value2);
            }
        }
Пример #2
0
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="cmd">命令文本</param>
        /// <param name="action">输出处理</param>
        public static void Exec(string cmd, Action <char> action)
        {
            //找出可执行文件和参数的分隔符的位置和长度
            ValueSet <int, int> result = cmd.IndexOfWhiteSpace();

            //创建进程
            using (Process p = new Process())
            {
                //如果没有参数
                if (result.IsNull())
                {
                    p.StartInfo.FileName = cmd;
                }
                //如果有参数
                else
                {
                    p.StartInfo.FileName  = cmd.Substring(0, result.Value1);
                    p.StartInfo.Arguments = cmd.Substring(result.Value1 + result.Value2);
                }

                //不使用 shell 启动进程
                p.StartInfo.UseShellExecute = false;
                //重定向输出
                p.StartInfo.RedirectStandardOutput = true;
                //重定向错误
                p.StartInfo.RedirectStandardError = true;

                //启动进程
                p.Start();

                if (action.IsNotNull())
                {
                    //读取输出
                    Cmd.Output(p.StandardOutput, action);

                    //读取错误
                    Cmd.Output(p.StandardError, action);
                }
            }
        }