Code archives/Miscellaneous/Permanent Environment Variables

This code has been declared by its author to be Public Domain code.

Download source code

Permanent Environment Variables by JoshK2012
If you use SetEnv, the environment variable is only visible to your program. Use this for permanent vars other apps can see. Must be run in admin mode.

You can use this to make your program request admin mode:
http://www.restuner.com/howto-insert-trust-info-manifest.htm
SuperStrict

SetEnv("MyAwesomeKey","Happy happy fun time")

Function SetEnv:Int(key:String,value:String)
	Local stream:TStream=WriteFile("temp.bat")
	If Not stream Return False
	stream.WriteLine "% reg add ~qHKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment~q /v ~q"+key+"~q /t REG_SZ /d ~q"+value+"~q"
	stream.close()
	Local proc:TProcess = CreateProcess("temp.bat")
	If Not proc Return False
	While proc.status()
		Delay 1
	Wend
	If getenv_(key)=value Return True
EndFunction

Comments

_PJ_2012
Doesn't SetEnv set the UserProfile environment variables?


dw8172016
I was intrigued at this and made a few changes so it will run in Windows 8.1, and yes - you are right. You have to run it under Administration Mode in order to get it to work.

Would be interested in seeing one that does not require this special method of execution.


Code Archives Forum