C#中 Process的扩展类ProcessExtensions

C#中 Process的扩展类ProcessExtensions

/// <summary>
    /// Process extensions
    /// </summary>
    public static class ProcessExtensions
    {
        #region Functions

#region KillProcessAsync

/// <summary>
        /// Kills a process
        /// </summary>
        /// <param>Process that should be killed</param>
        /// <param>Amount of time (in ms) until the process is killed.</param>
        public static void KillProcessAsync(this Process Process, int TimeToKill = 0)
        {
            if (Process == null)
                throw new ArgumentNullException("Process");
            ThreadPool.QueueUserWorkItem(delegate { KillProcessAsyncHelper(Process, TimeToKill); });
        }

/// <summary>
        /// Kills a list of processes
        /// </summary>
        /// <param>Processes that should be killed</param>
        /// <param>Amount of time (in ms) until the processes are killed.</param>
        public static void KillProcessAsync(this IEnumerable<Process> Processes, int TimeToKill = 0)
        {
            if (Processes == null)
                throw new ArgumentNullException("Processes");
            Processes.ForEach(x => ThreadPool.QueueUserWorkItem(delegate { KillProcessAsyncHelper(x, TimeToKill); }));
        }

#endregion

#region GetInformation

/// <summary>
        /// Gets information about all processes and returns it in an HTML formatted string
        /// </summary>
        /// <param>Process to get information about</param>
        /// <param>Should this be HTML formatted?</param>
        /// <returns>An HTML formatted string</returns>
        public static string GetInformation(this Process Process, bool HTMLFormat = true)
        {
            StringBuilder Builder = new StringBuilder();
            return Builder.Append(HTMLFormat ? "<strong>" : "")
                   .Append(Process.ProcessName)
                   .Append(" Information")
                   .Append(HTMLFormat ? "</strong><br />" : "\n")
                   .Append(Process.DumpProperties(HTMLFormat))
                   .Append(HTMLFormat ? "<br />" : "\n")
                   .ToString();
        }

/// <summary>
        /// Gets information about all processes and returns it in an HTML formatted string
        /// </summary>
        /// <param>Processes to get information about</param>
        /// <param>Should this be HTML formatted?</param>
        /// <returns>An HTML formatted string</returns>
        public static string GetInformation(this IEnumerable<Process> Processes, bool HTMLFormat = true)
        {
            StringBuilder Builder = new StringBuilder();
            Processes.ForEach(x => Builder.Append(x.GetInformation(HTMLFormat)));
            return Builder.ToString();
        }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wydspw.html