views:

881

answers:

5

Using this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

I get this output:

"C:\\Documents and Settings\\[USER]\\Application Data"

How can I get

"C:\\Documents and Settings\\[USER]\\"

?

+2  A: 

Try:

System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName/
Jay Riggs
This won't work on Windows 7, and I assume won't work on vista either if I remember the folder structure right. On 7, you'd have to go up 2 parent folders since the ApplicationData folder is \%userprofile%\AppData\Roaming\
Scott Ivey
That returns "C:\Users\Fredrik\AppData" on my Vista machine (the ApplicationData folder is "C:\Users\Fredrik\AppData\Roaming")
Fredrik Mörk
+6  A: 

Try:


System.Environment.GetEnvironmentVariable("USERPROFILE");
Thomas
It's a really bad idea to depend on environment variables to give you the folder paths. There are too many ways those environment variables can be changed. The recommended way is with Environment.SpecialFolder enumeration.
Jim Mischel
Unfortunately, as you can see http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx the USERPROFILE folder is not contained in that enumeration.
Thomas
+1  A: 
Environment.GetEnvironmentVariable("userprofile")

Trying to navigate up from a named SpecialFolder is prone for problems. There are plenty of reasons that the folders won't be where you expect them - users can move them on their own, GPO can move them, folder redirection to UNC paths, etc.

Using the environment variable for the userprofile should reflect any of those possible issues.

Scott Ivey
A: 

May be this will be a good solution: taking in account whether this is Vista/Win7 or XP and without using environment variables:

string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
if ( Environment.OSVersion.Version.Major >= 6 ) {
    path = Directory.GetParent(path);
}

Though using the environment variable is much more clear.

Kaagle
+1  A: 

Messing around with environment variables or hard-coded parent folder offsets is never a good idea when there is a API to get the info you want, call SHGetSpecialFolderPath(...,CSIDL_PROFILE,...)

Anders