当前位置: 首页>>代码示例>>C#>>正文


C# ICommand.Validate方法代码示例

本文整理汇总了C#中ICommand.Validate方法的典型用法代码示例。如果您正苦于以下问题:C# ICommand.Validate方法的具体用法?C# ICommand.Validate怎么用?C# ICommand.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ICommand的用法示例。


在下文中一共展示了ICommand.Validate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Execute

        /// <summary>
        /// Executes the given executable to process the given command.
        /// </summary>
        /// <param name="workingDirectory">
        /// The working directory while executing the command.
        /// </param>
        /// <param name="executable">
        /// The full path to and name of the executable to execute.
        /// </param>
        /// <param name="command">
        /// The options to the executable.
        /// </param>
        /// <param name="environmentVariables">
        /// An array of <see cref="KeyValuePair{TKey,TValue}"/> objects, containing environment variable
        /// overrides to use while executing the executable.
        /// </param>
        /// <param name="specialArguments">
        /// Any special arguments to pass to the executable, not defined by the <paramref name="command"/>
        /// object, typically common arguments that should always be passed to the executable.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="workingDirectory"/> is <c>null</c> or empty.</para>
        /// <para>- or -</para>
        /// <para><paramref name="executable"/> is <c>null</c> or empty.</para>
        /// <para>- or -</para>
        /// <para><paramref name="command"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="environmentVariables"/> is <c>null</c>.</para>
        /// <para>- or -</para>
        /// <para><paramref name="specialArguments"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="MercurialException">
        /// <para>The executable did not finish in the allotted time.</para>
        /// </exception>
        public static void Execute(
            string workingDirectory, string executable, ICommand command, KeyValuePair<string, string>[] environmentVariables,
            IEnumerable<string> specialArguments)
        {
            if (StringEx.IsNullOrWhiteSpace(workingDirectory))
                throw new ArgumentNullException("workingDirectory");
            if (StringEx.IsNullOrWhiteSpace(executable))
                throw new ArgumentNullException("executable");
            if (command == null)
                throw new ArgumentNullException("command");
            if (environmentVariables == null)
                throw new ArgumentNullException("environmentVariables");
            if (specialArguments == null)
                throw new ArgumentNullException("specialArguments");

            command.Validate();
            command.Before();

            IEnumerable<string> arguments = specialArguments;
            arguments = arguments.Concat(command.Arguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));
            arguments = arguments.Concat(command.AdditionalArguments.Where(a => !StringEx.IsNullOrWhiteSpace(a)));

            string argumentsString = string.Join(" ", arguments.ToArray());

            var psi = new ProcessStartInfo
            {
                FileName = executable,
                WorkingDirectory = workingDirectory,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false,
                ErrorDialog = false,
                Arguments = command.Command + " " + argumentsString,
            };
            foreach (var kvp in environmentVariables)
                psi.EnvironmentVariables[kvp.Key] = kvp.Value;
            psi.StandardErrorEncoding = _Encoding;
            psi.StandardOutputEncoding = _Encoding;

            if (command.Observer != null)
                command.Observer.Executing(command.Command, argumentsString);

            Process process = Process.Start(psi);
            try
            {
                Func<StreamReader, Action<string>, string> reader;
                if (command.Observer != null)
                {
                    reader = delegate(StreamReader streamReader, Action<string> logToObserver)
                    {
                        var output = new StringBuilder();
                        string line;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            logToObserver(line);
                            if (output.Length > 0)
                                output.Append(Environment.NewLine);
                            output.Append(line);
                        }
                        return output.ToString();
                    };
                }
                else
//.........这里部分代码省略.........
开发者ID:sbcfwebdev,项目名称:Target-Process-Plugins,代码行数:101,代码来源:CommandProcessor.cs


注:本文中的ICommand.Validate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。