目錄
1、為exe創建快捷方式
?2、實現軟件自啟動
3、完整代碼
4、結果展示
1、為exe創建快捷方式
利用winshell庫的CreateShortCut函數進行創建
【python學習】——獲取桌面路徑,獲取系統盤符,獲取電腦用戶名,獲取軟件自啟動存放目錄_有情懷的機械男的博客-CSDN博客【python學習】——獲取桌面路徑,獲取系統盤符,獲取電腦用戶名,獲取軟件自啟動存放目錄https://blog.csdn.net/qq_45769063/article/details/124707721
?2、實現軟件自啟動
自己可以創建一個快捷方式然后拖到電腦自啟動目錄也行
這里的話其實就是利用Python在電腦自啟動目錄下創建了一個exe的快捷方式而已
3、完整代碼
import os
import winreg
import winshell"""
這里調用了winshell的CreateShortcut函數。
傳入4個參數,分別為:快捷方式的路徑,exe文件的路徑,圖標路徑,還有描述信息。
"""def create_shortcut(bin_path: str, name: str, desc: str):''':param bin_path: exe路徑:param name: 需要創建快捷方式的路徑:param desc: 描述,鼠標放在圖標上面會有提示:return:'''try:print(winshell.desktop())shortcut = name + ".lnk"winshell.CreateShortcut(Path=shortcut,Target=bin_path,Icon=(bin_path, 0),Description=desc)return Trueexcept ImportError as err:print("Well, do nothing as 'winshell' lib may not available on current os")print("error detail %s" % str(err))return Falsedef get_desktop():key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')return winreg.QueryValueEx(key, "Desktop")[0]if __name__ == "__main__":import getpassimport os# 將快捷方式添加到桌面deskTopDir = get_desktop()# deskTopDir = winshell.desktop()bin_path = r"main_logic.exe"link_path = deskTopDir + "\\main_logic"desc = "喝水提醒小工具"create_shortcut(bin_path, link_path, desc)# 將快捷方式添加到自啟動目錄## #獲取用戶名username = getpass.getuser()## 系統盤符名稱syspath = os.getenv("SystemDrive")## 自啟動目錄startupPath = os.path.join(os.getenv("SystemDrive"),r"\users",getpass.getuser(),r"AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup")bin_path = r"main_logic.exe"link_path = startupPath + "\\main_logic"desc = "喝水提醒小工具"create_shortcut(bin_path, link_path, desc)
4、結果展示
1)、桌面快捷方式——鼠標放上面有信息提示
?2)、自啟動目錄快捷方式——鼠標放上面有信息提示
?
?