Qt学习笔记-Qt5程序开机自启动(windows)

原博文地址为:https://blog.csdn.net/x356982611/article/details/53183144


简介

window下开机启动最简单的实现方式就是在注册表中添加启动项目 添加位置有两个
  • 1
  • 2
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
  • 1
  • 2

第一个为全局自动启动项,所有用户登陆后都自动启动。第二个为当前用户启动项、切换到其他用户就失效了

代码

void appAutoRun(bool bAutoRun)
{
    //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
    QSettings  reg("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);

    if (bAutoRun)
    {
         QString strAppPath=QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
        //strAppPath.replace(QChar('/'),QChar('\\'),Qt::CaseInsensitive);
        reg->setValue("wirtepad",strAppPath);
    }
    else
    {
        reg->setValue("wirtepad","");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

提示

win7以后权限管理越来越严格,非管理员权限运行的程序是无法写入到HKEY_LOCAL_MACHINE下的,推荐添加启动项到当前用户

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/80459382