Windows 10注册表中路径项的地址?

2024-09-24 22:17:55 发布

您现在位置:Python中文网/ 问答频道 /正文

What is the correct address in the Windows 10 Registry from which to get and set the PATH variable?

当我运行下面的Python程序时,以下三个选项都不起作用:

from winreg import *
#The following line (uncommented) gives a list of things including Environment  
keyVal = r"SYSTEM\CurrentControlSet\Control\Session Manager"  
#The following line (uncommendted) gives an empty list of results (nothing)  
#keyVal = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"  
#The following line (uncommented) gives an error message as follows: "FileNotFoundError: [WinError 2] The system cannot find the file specified"  
#keyVal = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path"  
aKey = OpenKey(HKEY_LOCAL_MACHINE, keyVal, 0, KEY_ALL_ACCESS)  
try:  
    i = 0  
    while True:  
        asubkey = EnumKey(aKey, i)  
        print(asubkey)  
        i += 1  
except WindowsError:  
    pass  

调用上面的Python代码时,Windows CMD正在以管理员身份运行。我以管理员身份运行Windows CMD,希望在运行上述脚本访问注册表项时避免权限问题

Note: When I type regedit into the Windows Start Menu, and I drill down to Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment , I am able to see a properly-populated Path variable. Even though the error shown above is given when I try to access this programmatically as an administrator.


2nd Note: When I try the code at this link per @SimonCrane 's suggestion, and call the function with open_env_registry_key('system') , the result is an error when SYS_ENV_SUBPATH = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", or no results at all when using SYS_ENV_SUBPATH = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment" or SYS_ENV_SUBPATH = r"SYSTEM\CurrentControlSet\Control\Session Manager",


Tags: andthetoanenvironmentissessionwindows