import winreg
def set_autostart_registry[app_name, key_data=None, autostart: bool = True] -> bool:
"""
Create/update/delete Windows autostart registry key
! Windows ONLY
! If the function fails, OSError is raised.
:param app_name: A string containing the name of the application name
:param key_data: A string that specifies the application path.
:param autostart: True - create/update autostart key / False - delete autostart key
:return: True - Success / False - Error, app name dont exist
"""
with winreg.OpenKey[
key=winreg.HKEY_CURRENT_USER,
sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
reserved=0,
access=winreg.KEY_ALL_ACCESS,
] as key:
try:
if autostart:
winreg.SetValueEx[key, app_name, 0, winreg.REG_SZ, key_data]
else:
winreg.DeleteValue[key, app_name]
except OSError:
return False
return True
def check_autostart_registry[value_name]:
"""
Check Windows autostart registry status
! Windows ONLY
! If the function fails, OSError is raised.
:param value_name: A string containing the name of the application name
:return: True - Exist / False - Not exist
"""
with winreg.OpenKey[
key=winreg.HKEY_CURRENT_USER,
sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
reserved=0,
access=winreg.KEY_ALL_ACCESS,
] as key:
idx = 0
while idx < 1_000: # Max 1.000 values
try:
key_name, _, _ = winreg.EnumValue[key, idx]
if key_name == value_name:
return True
idx += 1
except OSError:
break
return False
Tạo Autostart:
set_autostart_registry['App name', r'C:\test\x.exe']
Cập nhật Autostart:
set_autostart_registry['App name', r'C:\test\y.exe']
Xóa Autostart:
set_autostart_registry['App name', autostart=False]
Kiểm tra Autostart:
if check_autostart_registry['App name']:
Cải thiện bài viết
Lưu bài viết
Thêm một tập lệnh Python vào Windows Start-up về cơ bản có nghĩa là tập lệnh Python sẽ chạy khi Windows khởi động. Điều này có thể được thực hiện theo quy trình hai bước -
Bước #1: Thêm tập lệnh vào thư mục khởi động Windows & NBSP; sau khi Windows khởi động, nó chạy [tương đương với nhấp đúp] tất cả các ứng dụng có trong thư mục khởi động của mình. & NBSP;
After the windows boots up it runs [equivalent to double-clicking] all the application present in its startup directory.
Địa chỉ: & nbsp;
C: \ Users \ current_user \ appdata \ roaming \ Microsoft \ Windows \ start
Theo mặc định, thư mục AppData trong current_user được ẩn, vì vậy hãy bật các tệp ẩn để lấy nó và dán phím tắt của tập lệnh vào địa chỉ đã cho hoặc chính tập lệnh. Ngoài ra, mặc định của các tệp .py phải được đặt thành python ide khác, tập lệnh có thể sẽ mở dưới dạng văn bản thay vì thực thi. & Nbsp; & nbsp; & nbsp; Đúng, nó liên quan đến việc chỉnh sửa khóa Windows Registry HKEY_CURRENT_USER từ chính tập lệnh Python. Đăng ký này chứa danh sách các chương trình phải chạy khi người dùng đăng nhập. Cũng giống như một vài ứng dụng bật lên khi Windows bắt đầu vì sự thay đổi trong sổ đăng ký và thêm đường dẫn ứng dụng của chúng vào nó..PY files default must be set to python IDE else the script may end up opening as a text instead of executing.
Step #2:
Adding script to windows Registry
This process can be risky if not done properly, it involves editing the windows registry key HKEY_CURRENT_USER from the python script itself. This registry contains the list of programs that must run once the user Login. just like few application which pops up when windows starts because the cause change in registry and add their application path to it.
Đường dẫn đăng ký:
HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ currentVersion \ Run
Dưới đây là mã Python: & NBSP;
Python3
import
winreg as reg
import
os
def
set_autostart_registry['App name', r'C:\test\x.exe']
0
set_autostart_registry['App name', r'C:\test\x.exe']
1set_autostart_registry['App name', r'C:\test\x.exe']
2set_autostart_registry['App name', r'C:\test\x.exe']
3 set_autostart_registry['App name', r'C:\test\x.exe']
4
set_autostart_registry['App name', r'C:\test\x.exe']
1set_autostart_registry['App name', r'C:\test\x.exe']
6set_autostart_registry['App name', r'C:\test\x.exe']
3set_autostart_registry['App name', r'C:\test\x.exe']
8
set_autostart_registry['App name', r'C:\test\x.exe']
1set_autostart_registry['App name', r'C:\test\y.exe']
0set_autostart_registry['App name', r'C:\test\x.exe']
3set_autostart_registry['App name', r'C:\test\y.exe']
2
set_autostart_registry['App name', r'C:\test\x.exe']
1set_autostart_registry['App name', r'C:\test\y.exe']
4set_autostart_registry['App name', r'C:\test\x.exe']
3 set_autostart_registry['App name', r'C:\test\y.exe']
6
set_autostart_registry['App name', r'C:\test\x.exe']
1set_autostart_registry['App name', r'C:\test\y.exe']
8set_autostart_registry['App name', r'C:\test\x.exe']
3 set_autostart_registry['App name', autostart=False]
0
set_autostart_registry['App name', r'C:\test\x.exe']
1set_autostart_registry['App name', autostart=False]
2 set_autostart_registry['App name', r'C:\test\x.exe']
3 set_autostart_registry['App name', autostart=False]
4set_autostart_registry['App name', autostart=False]
5set_autostart_registry['App name', autostart=False]
6
set_autostart_registry['App name', r'C:\test\x.exe']
1set_autostart_registry['App name', autostart=False]
8set_autostart_registry['App name', autostart=False]
2if check_autostart_registry['App name']:
0if check_autostart_registry['App name']:
1if check_autostart_registry['App name']:
0set_autostart_registry['App name', autostart=False]
5if check_autostart_registry['App name']:
4
set_autostart_registry['App name', r'C:\test\x.exe']
1if check_autostart_registry['App name']:
6set_autostart_registry['App name', autostart=False]
2if check_autostart_registry['App name']:
8
if check_autostart_registry['App name']:
9 import
0set_autostart_registry['App name', r'C:\test\x.exe']
3set_autostart_registry['App name', r'C:\test\x.exe']
3import
3import
4
set_autostart_registry['App name', r'C:\test\x.exe']
1import
6
Lưu ý: Có thể thêm các mã tiếp theo vào tập lệnh này để nhiệm vụ được thực hiện ở mỗi lần khởi động và tập lệnh phải được chạy với tư cách quản trị viên lần đầu tiên. & NBSP;Further codes can be added to this script for the task to be performed at every startup and the script must be run as Administrator for the first time.