简体   繁体   中英

In Windows Vista and 7, I can't access the %DEFAULTUSERPROFILE% system variable - it shows as not found

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?

您是否尝试过%ALLUSERSPROFILE%

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)

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;
}

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?

使用CSIDL_PROFILE和-1作为令牌参数调用SHGetFolderLocation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM