示例:设置程序为开机启动项


用途:如题


  #region - 开机启动项 -

        /// <summary> 开机启动项 </summary> 
        /// <param name=\"Started\">是否启动</param> 
        /// <param name=\"name\">启动值的名称</param> 
        /// <param name=\"path\">启动程序的路径</param> 
        public void RunWhenStart(bool Started, string name, string path)
        {
            using (RegistryKey hklm = Registry.LocalMachine)
            {
                RegistryKey run = hklm.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\", true);

                if (Started == true)
                {
                    // HTodo  :已经设置开机启动则返回 
                    if (this.IsAutoRun(name)) return;

                    run.SetValue(name, path);
                }
                else
                {
                    run.DeleteValue(name);
                }
            }
        }

        /// <summary> 检查是否是开机启动 </summary>
        public bool IsAutoRun(string name)
        {
            RegistryKey hklm = Registry.LocalMachine;
            //RegistryKey run = hklm.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");

            RegistryKey run = hklm.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");

            object obj = run.GetValue(name);

            if (obj.IsNull()) return false;

            return true;

        }


        #endregion


猜你喜欢

转载自blog.csdn.net/u010975589/article/details/61199117