首页 > Windows开发 > 详细

开机自启动(C#)

时间:2015-12-03 14:03:04      阅读:269      评论:0      收藏:0      [点我收藏+]
        SetAutoRun(Application.ExecutablePath, true);

        public void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey HKLM = Registry.CurrentUser;
            RegistryKey reg = null;
            try
            {
                if (!System.IO.File.Exists(fileName))
                    throw new Exception("该文件不存在!");
                String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = HKLM.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = HKLM.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (isAutoRun)
                    reg.SetValue(name, fileName);
                else
                    reg.SetValue(name, false);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }
        }

 

开机自启动(C#)

原文:http://www.cnblogs.com/margin-gu/p/5015792.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!