Qt在Windows上增加开机自启

#include <QSettings>
#include <QApplication>

#define REG_RUN "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"

static void setAutoStart(bool is_auto_start)
{
  QString application_name = QApplication::applicationName();
  QSettings *settings = new QSettings(REG_RUN, QSettings::NativeFormat);

  if(is_auto_start) {
    QString application_path = QApplication::applicationFilePath();
    settings->setValue(application_name, application_path.replace("/", "\\"));
  } else {
    settings->remove(application_name);
  }

  delete settings;
}

以上的REG_RUN路径是最为常用的开机自启路径,还可以在其它注册表路径中增加开机自启

©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页