日期:2014-05-18  浏览次数:20728 次

c#下实现程序开机自动启动
 /// <summary>
        /// 设置程序开机启动
        /// 或取消开机启动
        /// </summary>
        /// <param name="started">设置开机启动,或者取消开机启动</param>
        /// <param name="exeName">注册表中程序的名字</param>
        /// <param name="path">开机启动的程序路径</param>
        /// <returns>开启或则停用是否成功</returns>
        public static bool runWhenStart(bool started, string exeName, string path)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
            }
            if (started == true)
            {
                try
                {
                    key.SetValue(exeName, path);//设置为开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                try
                {
                    key.DeleteValue(exeName);//取消开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }