Search

Get Process Information

//Get Process Infomation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace IndiLogix.ProcInf
{
    struct ProcInfo
    {
        internal string PName, PWinTitle, PMscNm, PMnMod, PMmFnm;
        internal int PId, PBsPri, PExCod, PHwCnt, PMModMemSz, PSsnId, PThdCnt;
        internal DateTime PExTim, PStrTime;
        internal IntPtr PHwnd, PMmBsAdd, PMmEnPoAd, PMnWinHw, PMxWrkSt, PMinWrkSet, PProAff;
        internal bool PHsExt, PResp, PPriBooEnb;
        internal FileVersionInfo PMmFlVer;
        internal TimeSpan PToProTm, PUsrProTim, PPrvProTim;
        internal long PNonPgSysMemSiz, PWrkSet, PVirMemSiz, PPgMemSiz, PPgSysMemSiz, PPekPgMemSiz, PPekVirMemSiz, PPekWrkSet, PPvtMemSiz;

    };

    class I_ProcInfo
    {
        private const string usage = @"This is process Framework form IndiLogix, 
    It shows various process info";

        private ProcInfo pn;

        public string AboutIt
        {
            get { return usage; }
        }
        #region Process Property
        public int ProcessId { get { return pn.PId; } }
        public string MainWindowTitle { get { return pn.PWinTitle; } }
        public string ProcessName { get { return pn.PName; } }
        public int BasePriority { get { return pn.PBsPri; } }
        public int ExitCode { get { return pn.PExCod; } }
        public IntPtr Handle { get { return pn.PHwnd; } }
        public int HandleCount { get { return pn.PHwCnt; } }
        public bool HasExited { get { return pn.PHsExt; } }
        public string MachineName { get { return pn.PMscNm; } }
        public string Mod_Filename { get { return pn.PMmFnm; } }
        public FileVersionInfo Mod_Verinfo { get { return pn.PMmFlVer; } }
        public int Mod_MemorySize { get { return pn.PMModMemSz; } }
        public IntPtr Mod_BaseAddress { get { return pn.PMmBsAdd; } }
        public IntPtr Mod_EntryPointAddress { get { return pn.PMmEnPoAd; } }
        public string Mod_ModuleName { get { return pn.PMnMod; } }
        public IntPtr MainWindowHandle { get { return pn.PMnWinHw; } }
        public IntPtr MaxWorkingSet { get { return pn.PMxWrkSt; } }
        public IntPtr MinWorkingSet { get { return pn.PMinWrkSet; } }
        public bool Responding { get { return pn.PResp; } }
        public int SessionId { get { return pn.PSsnId; } }
        public int ThreadsCount { get { return pn.PThdCnt; } }
        public TimeSpan TotalProcessTIme { get { return pn.PToProTm; } }
        public TimeSpan UserProcessTIme { get { return pn.PUsrProTim; } }
        public long NonPagedSysMemory { get { return pn.PNonPgSysMemSiz; } }
        public long PageMemorySize { get { return pn.PPgMemSiz; } }
        public long PagedSystemMemorySize { get { return pn.PPgSysMemSiz; } }
        public long PeakPagedMemorySize { get { return pn.PPekPgMemSiz; } }
        public long PeakVirtualMemorySize { get { return pn.PPekVirMemSiz; } }
        public long PeakWorkingSet { get { return pn.PPekWrkSet; } }
        public bool PriorityBoostEnabled { get { return pn.PPriBooEnb; } }
        public long PrivateMemorySize { get { return pn.PPvtMemSiz; } }
        public TimeSpan PrivilegedProcessTime { get { return pn.PPrvProTim; } }
        public IntPtr ProcessorAffinity { get { return pn.PProAff; } }
        public DateTime StartTime { get { return pn.PStrTime; } }
        public long VirtualMemorySize { get { return pn.PVirMemSiz; } }
        public long WorkingSet { get { return pn.PWrkSet; } }
        #endregion

        public void I_Kill(string ProcName)
        {
            try
            {
                Process[] processByName = Process.GetProcessesByName(ProcName);
                //Process pogo = Process.GetProcesses(); 
                foreach (Process pr in processByName)
                {
                    pr.Kill();
                }
            }
            catch
            {
                Console.WriteLine("Cannot terminate " + ProcName + "\n");
            }
        }

        public void I_Kill(int Id)
        {
            try
            {
                Process processById = Process.GetProcessById(Id);
                processById.Kill();
                //foreach (Process pr in processByName) 
                //{ 
                //    pr.Kill(); 
                //} 
            }
            catch
            {
                Console.WriteLine("Cannot terminate " + Id + "\n");
            }
        }

        public static void I_GetRunningProc()
        {
            Process[] go = Process.GetProcesses();
            foreach (Process g in go)
            {
                Console.WriteLine("{0}|{1}|{2}", g.ProcessName, g.MainWindowTitle, g.Id.ToString());
            }
        }

        public string I_GetProcName(int Id)
        {
            string res = "Process Not Found";
            Process[] pro = Process.GetProcesses();
            foreach (Process p in pro)
            {
                if (p.Id.Equals(Id))
                {
                    res = p.ProcessName;
                    break;
                }
                else
                {
                    res = "Process Not Found";
                }
            }
            return res;
        }

        public int I_GetProcId(string pnm)
        {
            int res = 0;
            Process[] pro = Process.GetProcesses();
            foreach (Process p in pro)
            {
                if (p.ProcessName.Equals(pnm))
                {
                    res = p.Id;
                    break;
                }
                else
                {
                    res = 0;
                }
            }
            return res;
        }
        public void I_GetProcInfo(string ProcessName)
        {
            Process[] go = Process.GetProcesses();
            foreach (Process g in go)
            {
                if (g.ProcessName.Equals(ProcessName))
                {
                    pn.PName = g.ProcessName;
                    pn.PWinTitle = g.MainWindowTitle;
                    pn.PId = g.Id;
                    pn.PBsPri = g.BasePriority;
                    //pn.PExCod = g.ExitCode; 
                    //pn.PExTim = g.ExitTime; 
                    pn.PHwnd = g.Handle;
                    pn.PHwCnt = g.HandleCount;
                    pn.PHsExt = g.HasExited;
                    pn.PMscNm = g.MachineName;
                    pn.PMmFnm = g.MainModule.FileName;
                    pn.PMmFlVer = g.MainModule.FileVersionInfo;
                    pn.PMModMemSz = g.MainModule.ModuleMemorySize;
                    pn.PMmBsAdd = g.MainModule.BaseAddress;
                    pn.PMmEnPoAd = g.MainModule.EntryPointAddress;
                    pn.PMnMod = g.MainModule.ModuleName;
                    pn.PMnWinHw = g.MainWindowHandle;
                    pn.PMxWrkSt = g.MaxWorkingSet;
                    pn.PMinWrkSet = g.MinWorkingSet;
                    pn.PResp = g.Responding;
                    pn.PSsnId = g.SessionId;
                    pn.PThdCnt = g.Threads.Count;
                    pn.PToProTm = g.TotalProcessorTime;
                    pn.PUsrProTim = g.UserProcessorTime;
                    pn.PNonPgSysMemSiz = g.NonpagedSystemMemorySize64;
                    pn.PPgMemSiz = g.PagedMemorySize64;
                    pn.PPgSysMemSiz = g.PagedSystemMemorySize64;
                    pn.PPekPgMemSiz = g.PeakPagedMemorySize64;
                    pn.PPekVirMemSiz = g.PeakVirtualMemorySize64;
                    pn.PPekWrkSet = g.PeakWorkingSet64;
                    pn.PPriBooEnb = g.PriorityBoostEnabled;
                    pn.PPvtMemSiz = g.PrivateMemorySize64;
                    pn.PPrvProTim = g.PrivilegedProcessorTime;
                    pn.PProAff = g.ProcessorAffinity;
                    pn.PStrTime = g.StartTime;
                    pn.PVirMemSiz = g.VirtualMemorySize64;
                    pn.PWrkSet = g.WorkingSet64;
                }
            }


        }

    }
}