注册表操作

注册表操作

  //写入启动项
        private void btnStart_Click(object sender, EventArgs e)
        {
            //获取程序执行路径..
            string starupPath = Application.ExecutablePath;
            //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
            RegistryKey loca = Registry.LocalMachine;
            RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

            try
            {
                //SetValue:存储值的名称
                run.SetValue("kaixin", starupPath);
                MessageBox.Show(run.GetValue("kaixin").ToString());
                MessageBox.Show("已启用开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loca.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            regset();//获取注册表状态
        }

        //删除启动项
        private void btnStop_Click(object sender, EventArgs e)
        {
            //获取程序执行路径..
            string starupPath = Application.ExecutablePath;
            //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
            RegistryKey loca = Registry.LocalMachine;
            RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

            try
            {
                //SetValue:存储值的名称
                run.DeleteValue("kaixin");
                MessageBox.Show("已停止开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loca.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            regset();//获取注册表状态
        }

        //状态
        private void regset()
        {
            //获取程序执行路径..
           // string starupPath = Application.ExecutablePath;
            //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
            RegistryKey loca = Registry.LocalMachine;
            RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            string[] subkeyNames;
            subkeyNames = run.GetValueNames();
            foreach (string keyName in subkeyNames)
            {
                if (keyName == "kaixin")
                {
                    this.lblMsg.Text = "开机运行(启动中)";
                    this.btnStart.Enabled = false;
                    this.btnStop.Enabled = true;
                }
                else
                {
                    this.lblMsg.Text = "开机运行(已停止)";
                    this.btnStart.Enabled = true;
                    this.btnStop.Enabled = false;
                }
            }
        }

 

    /// <summary>
        /// 根据指定注册表路径启动程序
        /// </summary>
        private static void GetPathStart()
        {
            RegistryKey loca = Registry.LocalMachine;
            //路径可自定义
            RegistryKey GetPath = loca.CreateSubKey(@"SOFTWARE\Microsoft\VisualStudio\9.0\Setup");
            string Path = GetPath.GetValue("Dbghelp_path").ToString();
            Process.Start(Path + @"\devenv.exe");
        }

 

你可能感兴趣的:(windows,Microsoft)