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?
views:
80answers:
5for 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
2010-05-25 18:46:03
ALLUSERSPROFILE is shared data for all users, the default profile is the basis for new user profiles
Anders
2010-05-26 19:26:00
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
2010-05-25 18:40:08
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
2010-05-25 18:47:08
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
2010-05-25 18:50:11
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
2010-05-25 18:45:58
%DefaultUserProfile%, according to Microsoft, points to HKLM\MicrsoftWindows NT\CurrentVersion\ProfileList [Default]
shifuimam
2010-05-25 18:51:13
+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
2010-05-25 18:55:40
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
2010-05-25 19:16:50
+1
A:
Call SHGetFolderLocation with CSIDL_PROFILE and -1 as the token parameter
w0lo
2010-05-26 19:12:16