[置顶] C#中 Process的扩展类ProcessExtensions

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

        #region KillProcessAsync

        /// <summary>
        /// Kills a process
        /// </summary>
        /// <param name="Process">Process that should be killed</param>
        /// <param name="TimeToKill">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 name="Processes">Processes that should be killed</param>
        /// <param name="TimeToKill">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 name="Process">Process to get information about</param>
        /// <param name="HTMLFormat">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 name="Processes">Processes to get information about</param>
        /// <param name="HTMLFormat">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();
        }

        /// <summary>
        /// Get information of the process name
        /// </summary>
        /// <param name="Process">Process to get information about</param>
        /// <returns></returns>
        public static string GetInformation(this Process Process)
        {
            return Process.ProcessName;
        }

        /// <summary>
        /// Gets information about all processes and returns it in  string
        /// </summary>
        /// <param name="Processes">Processes to get information about</param>
        /// <returns>string</returns>
        public static string GetInformation(this IEnumerable<Process> Processes)
        {
            StringBuilder Builder = new StringBuilder();
            Processes.ForEach(x => Builder.Append(x.GetInformation()));
            return Builder.ToString();
        }

        #endregion

        #endregion

        #region Private Static Functions

        /// <summary>
        /// Kills a process asyncronously
        /// </summary>
        /// <param name="ProcessName">Name of the process to kill</param>
        /// <param name="TimeToKill">Amount of time until the process is killed</param>
        private static void KillProcessAsyncHelper(Process Process, int TimeToKill)
        {
            if (TimeToKill > 0)
                Thread.Sleep(TimeToKill);
            Process.Kill();
        }

        #endregion
    }

转载于:https://www.cnblogs.com/kevinGao/archive/2012/01/10/2776050.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值