How to make a python code run on windows startup/login any option of them?

Viewed 31

How to make a python code run on windows startup/login any option of them?

without task scheduler just want the code to add itself to startup.

I've searched and found these to options but none of them worked for me. sure there is a rest of the program that I'm writing but this is only the startup part!

if there is something missing in my question or something needs to be corrected tell me I'm not an expert of using stack overflow

import winreg as reg
import os

def addToRegistry():
    pth = os.path.dirname(os.path.realpath(__file__))

    s_name = "main.exe"
    address = os.path.join(pth, s_name)

    key = reg.HKEY_CURRENT_USER
    key_value = "Software\Microsoft\Windows\CurrentVersion\Run"
    
    open = reg.OpenKey(key, key_value, 0, reg.KEY_ALL_ACCESS)
    
    reg.SetValueEx(open, "any_name", 0, reg.REG_SZ, address)
    reg.CloseKey(open)


addToRegistry()
import getpass
import os
USER_NAME = getpass.getuser()


def add_to_startup(file_path=""):
    if file_path == "":
        file_path = os.path.dirname(os.path.realpath(__file__))
    bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup' % USER_NAME
    with open(bat_path + '\\' + "open.bat", "w+") as bat_file:
        bat_file.write(r'start "" "%s"' % file_path)

add_to_startup("")
0 Answers
Related