欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » C# » 正文

热门阅读

在C#中如何获取进程的父进程名称

创建时间:2012年09月09日 09:56  阅读次数:(13988)
分享到:
我们如何在C#中获取某进程的父进程的名称呢?下面是csdn上网友提供的答案,供参考:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
    public static class ProcessExtensions 
    { 
        private static string FindIndexedProcessName(int pid) 
        { 
            var processName = Process.GetProcessById(pid).ProcessName; 
            var processesByName = Process.GetProcessesByName(processName); 
            string processIndexdName = null; 
            for (var index = 0; index < processesByName.Length; index++)
            { 
                processIndexdName = index == 0 ? processName : processName + "#" + index;
                var processId = new PerformanceCounter("Process", "ID Process", processIndexdName); 
                if ((int)processId.NextValue() == pid) 
                { 
                    return processIndexdName; 
                } 
            } 
            return processIndexdName; 
        } 

        private static Process FindPidFromIndexedProcessName(string indexedProcessName) 
        { 
            var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName); 
            return Process.GetProcessById((int)parentId.NextValue()); 
        } 

        public static Process Parent(this Process process) 
        { 
            return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
        } 
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Process.GetCurrentProcess().Parent().ProcessName); 
        }
    }
}
,
来源:http://topic.csdn.net/u/20120908/22/635223ff-21e4-456f-a897-869d832691e0.html?98782
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 55+83=? 的结果(结果是:138)
结  果: