FrmOptionSet.cs 3.4 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ServiceManager
{
    public partial class FrmOptionSet : FrmBase
    {
        private AutoStart m_as = null;
        private string m_KeyDir = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
        private RegistryKey m_regLOCALMACHINE = Registry.LocalMachine;
        private string m_ValueName = "";
        private string m_ValueProperty = "";
        private string m_DefaultValueName = "Contamination ExplorerServiceManager";
        private string m_DefaultValueProperty = string.Empty;

        public FrmOptionSet()
        {
            InitializeComponent();
            this.m_ValueName = this.m_DefaultValueName;
            this.Init();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.cbAutoStart.Checked == true)
                {
                    if (!this.PropertyExisted(m_ValueProperty))
                    {
                        if (String.IsNullOrEmpty(this.CreateValue(this.m_regLOCALMACHINE, this.m_KeyDir, this.m_ValueName, m_ValueProperty).Trim()))
                            MessageBox.Show("设置失败", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }
                else
                {
                    this.DeleteValue(this.m_regLOCALMACHINE, this.m_KeyDir, this.m_ValueName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cbAutoStart_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = this.btnOK;
        }

        private string CreateValue(RegistryKey Root, string Dir, string ValueName, object ValueProperty)
        {
            return m_as.CreateValue(Root, Dir, ValueName, ValueProperty);
        }

        private bool IsExisted(string ValueName)
        {
            return m_as.ValueExisted(this.m_regLOCALMACHINE, this.m_KeyDir, ValueName);
        }

        private string GetValueNameByProperty(object ValueProperty)
        {
            return m_as.GetValueNameByProperty(this.m_regLOCALMACHINE, this.m_KeyDir, ValueProperty);
        }
        private bool PropertyExisted(object ValueProperty)
        {
            return m_as.PropertyExisted(this.m_regLOCALMACHINE, this.m_KeyDir, ValueProperty);
        }

        private bool DeleteValue(RegistryKey Root, string Dir, string ValueName)
        {
            return m_as.DeleteValue(Root, Dir, ValueName);
        }

        private void Init()
        {

            this.m_ValueProperty = Application.ExecutablePath + @" /n";
            m_as = new AutoStart();
            this.cbAutoStart.Select();
            this.cbAutoStart.Focus();
            if (this.PropertyExisted(m_ValueProperty))
            {
                this.cbAutoStart.CheckState = CheckState.Checked;
            }
            else
            {
                this.cbAutoStart.CheckState = CheckState.Unchecked;
            }
            this.StartPosition = FormStartPosition.CenterScreen;

        }
    }
}