1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import os import sys import winreg import ctypes import win32api import win32con
def auto_run(switch:bool=True) -> None: BaseName = os.path.basename(sys.argv[0]) if not ctypes.windll.shell32.IsUserAnAdmin(): ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, BaseName, None, 1) return
location = fr"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
def is_auto_run() ->bool: key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, location) i = 0 while True: try: if winreg.EnumValue(key, i)[0] == BaseName: return True i += 1 except OSError: winreg.CloseKey(key) break return False
Is_auto_run = is_auto_run() if switch: if not Is_auto_run: sys.setrecursionlimit(1000000) commnd = os.path.join(os.getcwd(),BaseName) name = BaseName key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, location, 0, win32con.KEY_ALL_ACCESS) win32api.RegSetValueEx(key, name, 0, win32con.REG_SZ, commnd) win32api.RegCloseKey(key) else: if Is_auto_run: key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, location, 0, win32con.KEY_ALL_ACCESS) win32api.RegDeleteValue(key, BaseName) win32api.RegCloseKey(key)
|