By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Get unresolved/raw environment variable

100+
P: 115
My PATH variable contains other enviornment variables. For example: My PATH is "%SOME_PATH%; C:\windows\ ..." and i have the varibale "SOME_PATH" which is.."C:\SomePath"..

When i run this code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. using (Microsoft.Win32.RegistryKey l_key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", false))
  3. {
  4. return (l_key.GetValue("PATH") as String);
  5. }
  6.  
I get "C:\SomePath;C:\windows\ ..." instead of "%SOME_PATH%; C:\windows\ ..."

How do i get the unresolved environment variable?
Jan 15 '09 #1
Share this Question
Share on Google+
2 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
Does Environment.GetEnvironmentVariable("path") work for you?
Jan 15 '09 #2

100+
P: 115
@insertAlias
It does not. It returns the expanded value. I noticed I forgot to mention in my post I was using .Net 1.1. Shortly after posting I found that RegistryKey.GetValue had an overload that takes Microsoft.Win32.RegistryValueOptions that can have the value of DoNotExpandEnvironmentNames. Exactly what i was looking for...except it doesnt exist in .Net 1.1. Heres the code i finally came around to that works in 1.1: (partialy stripped from .Net 2.0)

Expand|Select|Wrap|Line Numbers
  1.  
  2. [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueExW", SetLastError = true)]
  3. static extern int RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, out uint lpType, System.Text.StringBuilder lpData, ref uint lpcbData); 
  4.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. new System.Security.Permissions.EnvironmentPermission(System.Security.Permissions.PermissionState.Unrestricted).Demand();
  3. using (Microsoft.Win32.RegistryKey l_key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", false))
  4. {
  5. System.Reflection.FieldInfo l_reg_info = l_key.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[0];
  6. IntPtr lp_hklm = (IntPtr)l_reg_info.GetValue(l_key);
  7. uint lui_buffer = 0;
  8. System.Text.StringBuilder ls_ret = null;
  9. uint lui_type = 0;
  10. RegQueryValueEx(lp_hklm, as_variable, 0, out lui_type, ls_ret, ref lui_buffer);
  11. ls_ret = new System.Text.StringBuilder((int)lui_buffer);
  12. int success = RegQueryValueEx(lp_hklm, as_variable, 0, out lui_type, ls_ret, ref lui_buffer);
  13.  
  14. if (success == 0)
  15. return ls_ret.ToString();
  16. else
  17. throw new Exception("Error reading enviornment variable " + as_variable);
  18. }
  19.  
note: this only works for machine level environment variables
Jan 16 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.