I'm trying to get the actual values of environment variables.
This is what I have so far:
string query = string.Format("Select VariableValue From Win32_Environment Where Name = '{0}'", variableName);
using (var searcher = new ManagementObjectSearcher(query))
using (ManagementObject result = searcher.Get().Cast<ManagementObject>().FirstOrDefault())
{
if (result != null)
return Convert.ToString(result["VariableValue"]);
}
That works, but here's the problem: passing 'windir' as name gets '%SystemRoot%' as value. What I really want is the actual path, i.e. 'C:\Windows'.
I tried using recursion to get the value of 'SystemRoot' but no matches were found.
How can I make sure that the real values get returned?
Thx!
For system path variables (like
%SystemRoot%
) there's no convenient way.You have to look for these values yourself by reading the corresponding registry values. Heres' a (not complete) list of some of these system variables:
%SystemRoot%:
or%SystemDrive% can be determined by examining %SystemRoot%
Variables like %AppData% are user dependent and found under
You can't use Win32_Environment for this, but you can use remote registry.
use
Environment.GetFolderPath(Environment.SpecialFolder.System)
I know it's creative at best but this seems to be the simplest solution:
Too much overhead perhaps?