分享

唯一进程外部保护

 Jcstone 2016-12-27
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace FrontProtector
{
    public partial class FormMain : Form
    {
        private string FrontApp;
        private string FrontAppProcessName;

        public int Delay = 2;
        public bool IsCHN = false;

        public FormMain()
        {
            InitializeComponent();
            FrontApp = System.Configuration.ConfigurationManager.AppSettings["APP"];

            if (!File.Exists(FrontApp))
            {
                MessageBox.Show("程序不存在,请在配置文件设置好程序路径!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                MyClose();
            }

            FrontAppProcessName = Path.GetFileNameWithoutExtension(FrontApp);

            try
            {
                timer.Interval = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Interval"]);
                Delay = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Delay"]);
                IsCHN = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsCHN"]);
            }
            catch
            {             
 MessageBox.Show("请在配置文件设置正确参数!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        MyClose();
            }


            notifyIcon.MouseDoubleClick += new MouseEventHandler(notifyIcon_MouseDoubleClick);
            this.Disposed += new EventHandler(FormMain_Disposed);
            this.FormClosing += new FormClosingEventHandler(FormMain_FormClosing);
            timer.Enabled = true;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

            RegistRun();
            if (IsCHN)
            {
                this.label1.Text = string.Format("正在守护进程({0})运行......", FrontAppProcessName);
                this.Text = "进程守护工具";
            }
            else
            {
                this.label1.Text = string.Format("FrontProtector is protect process ({0}) running ......", FrontAppProcessName);
                this.Text = "FrontProtector";
            }
        }

        private static void RegistRun()
        {

            string startup = Application.ExecutablePath;
            RegistryKey rKey = Registry.LocalMachine;
            //开机自动运行
            RegistryKey autoRun = rKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            try
            {
                autoRun.SetValue("FrontProtector", startup);
                rKey.Close();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        void timer_Tick(object sender, EventArgs e)
        {
            ProtectProcess(FrontAppProcessName);
        }


        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            this.notifyIcon.Visible = true;
            e.Cancel = true;

        }

        void FormMain_Disposed(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }

        private void ProtectProcess(string FrontAppProcessName)
        {
            List<Process> FrontAppProcesses = new List<Process>();
            Process[] process = Process.GetProcesses();
            foreach (Process prc in process)
            {
                if (prc.ProcessName == FrontAppProcessName)
                    FrontAppProcesses.Add(prc);
            }

            if (FrontAppProcesses.Count > 1)
            {
                foreach (Process prc in process)
                {
                    if (prc.ProcessName.Contains(FrontAppProcessName))
                        prc.Kill();
                }

                System.Threading.Thread.Sleep(Delay);
                Process proExplorer = new Process();
                proExplorer.StartInfo.FileName = FrontApp;
                proExplorer.Start();

            }
            else if (FrontAppProcesses.Count < 1)
            {
                Process proExplorer = new Process();
                proExplorer.StartInfo.FileName = FrontApp;
                proExplorer.Start();
            }
        }

        private void MyClose()
        {
            this.Dispose();
            this.notifyIcon.Visible = false;
            notifyIcon.Dispose();
            Application.Exit();

        }



        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否退出进程守护系统?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                MyClose();
            }
        }

        /// <summary>
        /// 托盘双击还原窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }


    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约