监测进程

[DllImport("kernel32.dll")]
        private static extern int Process32First(IntPtr hSnapshot,
                                         ref PROCESSENTRY32 lppe);

        [DllImport("kernel32.dll")]
        private static extern int Process32Next(IntPtr hSnapshot,
                                        ref PROCESSENTRY32 lppe);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags,
                                                       uint th32ProcessID);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr hSnapshot);

        private const uint TH32CS_SNAPPROCESS = 0x00000002;

        [StructLayout(LayoutKind.Sequential)]
        private struct PROCESSENTRY32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExeFile;
        }
        public static bool IsProcessRunning(string applicationName,ref int count)
        {
            IntPtr handle = IntPtr.Zero;
            try
            {
                handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                PROCESSENTRY32 info = new PROCESSENTRY32();
                info.dwSize = (uint)System.Runtime.InteropServices.
                              Marshal.SizeOf(typeof(PROCESSENTRY32));
                int first = Process32First(handle, ref info);
                do
                {
                    //Console.WriteLine(info.szExeFile);
                    if (string.Compare(info.szExeFile,
                          applicationName, true) == 0)
                    {
                        count++;
                    }
                }
                while (Process32Next(handle, ref info) != 0);
            }
            catch
            {
                throw;
            }
            finally
            {
                CloseHandle(handle);
                handle = IntPtr.Zero;
            }
            return false;
        }


        #region 强制关闭进程
        /// <summary>
        /// 关闭进程
        /// </summary>
        /// <param name="processName">进程名</param>
        private static void KillProcess(string processName)
        {
            System.Diagnostics.Process[] myproc = System.Diagnostics.Process.GetProcessesByName(processName);
            foreach (var item in myproc)
            {
                if (item.ProcessName == processName)
                {
                    item.Kill();
                }
            }

        }



        //强制关闭最近打开的某个进程

        private static void KillRecentProcess(string processName)
        {
            System.Diagnostics.Process[] Proc = System.Diagnostics.Process.GetProcessesByName(processName);
            System.DateTime startTime = new DateTime();
            int m, killId = 0;
            for (m = 0; m < Proc.Length; m++)
            {
                if (startTime < Proc[m].StartTime)
                {
                    startTime = Proc[m].StartTime;
                    killId = m;
                }
            }
            if (Proc[killId].HasExited == false)
            {
                Proc[killId].Kill();
            }
        }
        #endregion

原文地址:https://www.cnblogs.com/jinhaoObject/p/5242648.html