Programmatically modifiy environment variables?

前端 未结 6 499
盖世英雄少女心
盖世英雄少女心 2020-12-05 05:37
  • Windows 7.
  • It\'s for my own machine, so it doesn\'t matter if it requires admin rights or something.
  • Preferably in Python or .NET, but I can learn a
6条回答
  •  醉梦人生
    2020-12-05 06:08

    if you want to permanently set environment variable, you can insert the new value into registry. eg with vbscript, add the path "c:\test" into PATH variable

    Set WshShell = WScript.CreateObject("WScript.Shell")
    strReg = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path"
    strSetting = WshShell.RegRead(strReg)
    strNewSetting = strSetting&";c\test"
    WshShell.RegWrite strReg, strNewSetting
    

    So, if you use Python or other languages, you can do the same thing using your language's own api/modules to read and write registry

提交回复
热议问题