桌面端程序开机自启动代码实现

Windows

写注册表实现开机自启动

注册表有两个位置可以写入:

1
2
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run //对于所有用户
HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run // 对于当前用户

Windows QT代码实现

MacOS

编写plist文件,拷贝到~/Library/LaunchAgents/,使用launchctl load/unload命令实现开机自启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.myapp.launcher</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/myapp.app/Contents/MacOS/myapp</string>
</array>
<key>RunAtLoad</key>
<true />
</dict>
</plist>

MacOS QT代码实现

Ubuntu

使用systemctl机制实现开机自启动

  1. 拷贝.service和.timer文件到~/.config/systemd/user/,使用systemctl –user enable/disable命令实现开机自启动;
  2. systemctl命令用于.timer文件,.timer文件用于定时执行.service文件,防止图形界面启动后,出现qxcbconnection: could not connect to display错误;

Ubuntu QT代码实现

把/usr/share/Application/下的.desktop文件拷贝到~/.config/autostart/下,实现开机自启动(未验证)