【问题标题】:Python: Change Cmd Startup folder programmaticallyPython:以编程方式更改 Cmd 启动文件夹
【发布时间】:2012-08-10 00:36:30
【问题描述】:

我正在尝试创建一个 python 程序来轻松更改我的 cmd 启动文件夹(而不是键入 cd ... 来导航到所需的文件)
但首先我需要弄清楚如何更改它而无需将 regedit.exe 键入 cmd。
在浏览了 python 文档之后,我得到了:

from winreg import*

a=OpenKey(HKEY_CURRENT_USER,"Software\Microsoft\Command Processor\\")
SetValue(HKEY_CURRENT_USER,"Software\Microsoft\Command Processor\\",REG_SZ,"cd\\the path that I want.")

此代码确实编辑了字符串值(我相信这就是它的名称)默认值。
但我需要它做的是编辑字符串值 Autorun
#我尝试了将 Autorun 放入该 SetValue 函数的不同方法,但没有奏效。
注意:默认和自动运行都在 HKEY_CURRENT_USER\Software\Microsoft\Command Processor 中。
我也试过了

SetValueEx(a,"Autorun",0,REG_SZ,"cd\\The path that I wantsss.")#Don't know if this is the right way to use it.  

但这给了我这个错误:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    SetValueEx(a,"Autorun",0,REG_SZ,"cd\\The path that I wantsss.")
WindowsError: [Error 5] Access is denied    

我使用 python 3.1 和 windows7
提前谢谢你。

【问题讨论】:

  • 尝试改用 SetValueEx。您可能需要使用 OpenKey 返回的句柄作为键。 (在线文档说 SetValueEx 的 value_name 参数是一个键名,但我猜这是一个错字。)
  • @HarryJohnston 我试过你的方式访问被拒绝了。我需要获得管理员的权限吗?我该怎么做。(注意:我是管理员)。
  • 您必须指定 KEY_WRITE 或 KEY_ALL_ACCESS 作为 OpenKey 的第四个参数。默认值为 KEY_READ,它为您提供只读访问权限。

标签: windows python-3.x cmd


【解决方案1】:

您必须使用 SetValueEx 并使用适当的访问权限(KEY_WRITE 或 KEY_ALL_ACCESS)打开密钥,如下所示:

from winreg import*

a=OpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Command Processor",0,KEY_WRITE)
SetValueEx(a,"Autorun",0,REG_SZ,"cd\\The path that I wantsss.")
CloseKey(a)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多