Last active
June 29, 2023 10:43
-
-
Save AlgorithmAlchemy/3ec35b0cf83402e17655feae3218f7f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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