Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlgorithmAlchemy/3ec35b0cf83402e17655feae3218f7f6 to your computer and use it in GitHub Desktop.
Save AlgorithmAlchemy/3ec35b0cf83402e17655feae3218f7f6 to your computer and use it in GitHub Desktop.
import winreg, os, sys
import winsound
# Путь к исполняемому файлу скрипта
script_path = os.path.abspath(__file__)
key_path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
key_name = "YourScriptName"
# Проверяем наличие записи в реестре
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_READ) as key:
value, _ = winreg.QueryValueEx(key, key_name)
except FileNotFoundError:
value = None
# Если запись уже существует, выводим сообщение и не создаем новую
if value is not None:
print("Запись уже существует в реестре")
else:
# Создаем запись в реестре для автозапуска
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path, 0, winreg.KEY_SET_VALUE) as key:
winreg.SetValueEx(key, key_name, 0, winreg.REG_SZ, f'"{sys.executable}" "{script_path}"')
print("Запись успешно создана в реестре")
except PermissionError as e:
print(f'{e}')
# Воспроизводим звуковое уведомление
duration = 1000 # milliseconds
frequency = 440 # Hz
winsound.Beep(frequency, duration)
import subprocess
subprocess.run(["msg", "*", "Невозможно добавить в авто-загрузку\nЗапустите от имени администратора!"])
# отправляем уведомление
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment