views:

80

answers:

5

If I try to access this system variable from the Run... dialog, Windows tells me the directory doesn't exist. Some system variables, like %SYSTEMROOT% and %USERPROFILE%, do work. Consequently, if I try to use a supposedly nonexistent variable like %DEFAULTUSERPROFILE% or %PROFILESFOLDER% in C#, I get nothing in return. Is there something special I need to do to get access to these variables?

+2  A: 

Have you tried %ALLUSERSPROFILE%?

JSBangs
for some inexplicable reason, that points to C:\ProgramData. I need to point to C:\Users\Default\AppData. I could get the profiles directory from the registry, but that seems like a bad idea for some reason.
shifuimam
ALLUSERSPROFILE is shared data for all users, the default profile is the basis for new user profiles
Anders
A: 

My suggestion is to retreive that value directly from the registry - in case you can't expand it:

public static string GetDefaultUserProfilePath() {
    string path = System.Environment.GetEnvironmentVariable("DEFAULTUSERPROFILE") ?? string.Empty;
    if (path.Length == 0) {
        using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")) {
            path = (string)key.GetValue("Default", string.Empty);
        }
    }
    return path;
}
Fredrik Johansson
Don't directly poke around in the registry trying to get stuff that has a standard API. You'll just create more headaches for Raymond Chen.
JSBangs
That worked - thanks!I'd still like to know why all the environment variables laid out in http://technet.microsoft.com/en-us/library/dd560744%28WS.10%29.aspx#BKMK_1 don't work, but I'll use the registry entry for now.
shifuimam
A: 

You mention C# - you can't use environment variables inside C# path strings, you need to replace them using System.Environment.

System.Environment.GetEnvironmentalVariable("USERPROFILE");

I haven't seen %DefaultUserProfile% before - should it point to the first username that was installed?

Chris S
%DefaultUserProfile%, according to Microsoft, points to HKLM\MicrsoftWindows NT\CurrentVersion\ProfileList [Default]
shifuimam
+2  A: 

I need to point to C:\Users\Default\AppData.

Are you sure? Be aware that this folder is used to populate the inital AppData directory for each new user added to the system.

If you want the actual shared application data directory in .NET, it's this:

String commonAppData = Environment.GetFolderPath(Environment.SpecialFolders.CommonApplicationData)
R. Bemrose
Well, what I'm doing is making an app to add games to the Games Explorer. It seems like if the game doesn't have its own GDF.dll (which is currently how I plan on making the app behave), the game has to be referenced by a shortcut either in \Users\$username\AppData\Local\Microsoft\Windows\GameExplorer\ or \Users\Default\...
shifuimam
+1  A: 

Call SHGetFolderLocation with CSIDL_PROFILE and -1 as the token parameter

w0lo