Get unresolved/raw environment variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShadowLocke
    New Member
    • Jan 2008
    • 116

    #1

    Get unresolved/raw environment variable

    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:\SomePat h"..

    When i run this code:

    Code:
     
    using (Microsoft.Win32.RegistryKey l_key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", false))
    {
    return (l_key.GetValue("PATH") as String);
    }
    I get "C:\SomePath;C: \windows\ ..." instead of "%SOME_PATH %; C:\windows\ ..."

    How do i get the unresolved environment variable?
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Does Environment.Get EnvironmentVari able("path") work for you?

    Comment

    • ShadowLocke
      New Member
      • Jan 2008
      • 116

      #3
      Originally posted by insertAlias
      Does Environment.Get EnvironmentVari able("path") work for you?
      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.Get Value had an overload that takes Microsoft.Win32 .RegistryValueO ptions that can have the value of DoNotExpandEnvi ronmentNames. 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)

      Code:
       
      [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueExW", SetLastError = true)]
      static extern int RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, out uint lpType, System.Text.StringBuilder lpData, ref uint lpcbData);
      Code:
       
      new System.Security.Permissions.EnvironmentPermission(System.Security.Permissions.PermissionState.Unrestricted).Demand();
      using (Microsoft.Win32.RegistryKey l_key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", false))
      {
      System.Reflection.FieldInfo l_reg_info = l_key.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[0];
      IntPtr lp_hklm = (IntPtr)l_reg_info.GetValue(l_key);
      uint lui_buffer = 0;
      System.Text.StringBuilder ls_ret = null;
      uint lui_type = 0;
      RegQueryValueEx(lp_hklm, as_variable, 0, out lui_type, ls_ret, ref lui_buffer);
      ls_ret = new System.Text.StringBuilder((int)lui_buffer);
      int success = RegQueryValueEx(lp_hklm, as_variable, 0, out lui_type, ls_ret, ref lui_buffer);
       
      if (success == 0)
      return ls_ret.ToString();
      else
      throw new Exception("Error reading enviornment variable " + as_variable);
      }
      note: this only works for machine level environment variables
      Last edited by ShadowLocke; Jan 16 '09, 03:07 PM. Reason: Forgot dllimport..

      Comment

      Working...