Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让程序进入Windows启动_Python_Startup - Fatal编程技术网

Python 让程序进入Windows启动

Python 让程序进入Windows启动,python,startup,Python,Startup,有没有一种方法可以直接在脚本中创建此函数,以便程序启动时自动进入启动状态 C:\Users\%NaMe%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 谢谢你的建议。首先,如果这是你自己的电脑,你可以直接到那个位置粘贴它,然后从任务管理器中启动它 但是,第二,如果您想在另一个脚本上执行,我建议使用类似这样的代码创建一个外部python文件 import winreg as reg import os

有没有一种方法可以直接在脚本中创建此函数,以便程序启动时自动进入启动状态 C:\Users\%NaMe%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup


谢谢你的建议。

首先,如果这是你自己的电脑,你可以直接到那个位置粘贴它,然后从任务管理器中启动它

但是,第二,如果您想在另一个脚本上执行,我建议使用类似这样的代码创建一个外部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()
您也可以将其添加到同一文件中,但这可能会导致不可预见的情况。您可以在此处或转到下面提到的链接了解更多信息并添加一些其他选项


这是否回答了您的问题?