Reading other user data from registry

Here is a summary of what I am doing:

1. Running C# application as user1

2. Retrieve ntuser.dat file for user2 and load as a hive in registry

3. Reading HKEY_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

My issue is that when performing #3. The ntuser.dat file for user2 has %USERPROFILE%... in it. When the application retrieves this it is returning the %USERPROFILE% variable for user1. So the results from the ntuser.dat file for user2 for Desktop is C:\Documents and Settings\User1\Desktop. This is not an acceptable result. I would be happy with either of the following scenarios:

a. The application returns the reg entry as-is: %USERPROFILE%\Desktop

b. The application is able to convert the %USERPROFILE% variable to the appropriate value for the given user: in this case user2 C:\Documents and Settings\User2\Desktop

Is there a way to accomplish this so I can read the correct desktop (also favorites, and My Documents) for a user other than the one running the application.

Hopefully this is not too confusing.... Thanks is advance.

- Adam



Answer this question

Reading other user data from registry

  • trev73

    For "expandable string values" in the registry, the framework automatically calls ExpandEnvironmentVariables for you. You'll have to use the GetValue override that takes the RegistryValueOptions parameter. If you're doing something like this:
    String value = regkey.GetValue("MyValueName");
    Then, you can change it to this:
    String value = regkey.GetValue("MyValueName", null, RegistryValueOptions.DoNotExpandEnvironmentVariables);

    Then you can expand the environment variables manually. Keep in mind that System.Environment.ExpandEnvironmentVariables will only correctly expand environment variables for the current user. If you want to use ExpandEnvironmentVariables, you'll have to impersonate the user whose environment you're interested in for it to work. Without impersonation you'll have to guess at what those user's environment variable values are.

  • truepantera

    Thank you for the relocation and the response. System.Environment.ExpandEnvrironmentVariables is not exactly what I need to do. It seems that the call to regkey.GetValue() is expanding the env variable for the currently logged in user but, the data I am pulling is for a different user and a different env variable. I need to be able to either pull the env variable for the specific user (not the one logged in) or display the env variable as-is (not expanded)

    - Adam


  • Bloom326984

    There wasn't anything C# language-specific about your question, I moved it from the C# Language forum to the C# General Forum.

  • Arkady Frenkel

    Use System.Environment.ExpandEnvironmentVariables to expand environment variables like "%userprofile%"

  • JCExTel

    Thank you very much. I didn't even notice the override... doh!

    The RegistryValueOptions.DoNotExpandEnvironmentNames does exactly what I need.

    - Adam


  • Reading other user data from registry