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

来源:互联网 发布:网络用语128是什么意思 编辑:程序博客网 时间:2024/06/15 10:19


用途:如题


  #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


0 0