Programmatically modifiy environment variables?

前端 未结 6 498
盖世英雄少女心
盖世英雄少女心 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 05:57

    Programmatically modifying environment variables is only for the duration of the program. Have not heard of actually modifying the environment system-wide and making it effective there and then. I do not think that can be done, that would require poking around at privileged level and possibly messing with the core system to achieve that.

    Even under Unix, it cannot be done despite some hacks to achieve it. I do remember seeing code that actually did modify the environment variables under MSDOS, by altering the MSDOS's _psp environment data structure, but that was a single-tasking system and 16bit with no protection whatsoever.

    To sum up, I do not think you can and it would be unwise to do so, it could be perceived as if the system is under a threat by a 'trojan' or a 'virus' as a result if attempting to do so, not alone that, as a user, I would not like for a program to modify the system environment variable without my consent! Sure, a program can write to the registry to make it permanent, but I would still like to know what is the purpose of it and why.

提交回复
热议问题