Hey, i want to use windows environment variables as value for a registry entry. Unfortunately i can not simply write sth like %systemroot%\system32\MyScreensaver.scr
.
As you can guess, i want to point some reg values to my own app, such as the auto start and screensaver and some other things.
Any ideas?
The Windows registry supports this natively with the REG_EXPAND_SZ
registry value type.
Just use REG_EXPAND_SZ
instead of REG_SZ
when you want to embed environment variables in the registry key value.
Here is an example of C# code accessing a REG_EXPAND_SZ
and the expansion is handled automatically:
var registry = Registry.CurrentUser.OpenSubKey("Environment");
var temp = registry.GetValue("TEMP") as string;
Here is an example of creating an expandable registry value:
registry.SetValue("TEMP", @"%USERPROFILE%\AppData\Local\Temp", RegistryValueKind.ExpandString);
Other platforms or scripting languages have other mechanisms to support this. Here is the low-level Win32 description of REG_EXPAND_SZ
: