让程序进入 Windows 启动

Getting the program into Windows startup

有没有办法直接在脚本中实现这个功能,让程序启动的时候自动进入startup C:\Users\%NaMe%\AppData\Roaming\Microsoft\Windows\开始菜单\程序\Startup.

谢谢你的想法。

首先,如果这是您自己的 PC,您可以实际转到该位置并将其粘贴,然后在启动时从任务管理器中创建 运行。

但是其次,如果您想在不同的脚本上执行操作,我建议使用类似这样的代码创建一个外部 python 文件。

import winreg as reg  
import os              

def AddToRegistry(): 

    # in python __file__ is the instant of 
    # file path where it was executed  
    # so if it was executed from desktop, 
    # then __file__ will be  
    # c:\users\current_user\desktop 
    pth = os.path.dirname(os.path.realpath(__file__)) 

    # name of the python file with extension 
    s_name="mYscript.py"     

    # joins the file name to end of path address 
    address=os.join(pth,s_name)  

    # key we want to change is HKEY_CURRENT_USER  
    # key value is Software\Microsoft\Windows\CurrentVersion\Run 
    key = HKEY_CURRENT_USER 
    key_value = "Software\Microsoft\Windows\CurrentVersion\Run"

    # open the key to make changes to 
    open = reg.OpenKey(key,key_value,0,reg.KEY_ALL_ACCESS) 

    # modifiy the opened key 
    reg.SetValueEx(open,"any_name",0,reg.REG_SZ,address) 

    # now close the opened key 
    reg.CloseKey(open) 

if __name__=="__main__": 
    AddToRegistry()

您也可以将其添加到同一个文件中,但这可能会导致不可预见的情况。您可以在此处 click 或转到下面提到的 link 以了解更多信息并添加一些其他选项。

https://www.geeksforgeeks.org/autorun-a-python-script-on-windows-startup/