129

Using this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

I get this output:

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

How can I get the root directory of all users? i.e.:

"C:\\Documents and Settings\\[USER]\\"
bytecode77
  • 14,163
  • 30
  • 110
  • 141
juan
  • 80,295
  • 52
  • 162
  • 195
  • 1
    The answer I ended up using was deleted... this works: Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) – juan Jul 16 '09 at 21:31
  • 2
    Juan - that won't always work for you - you just got lucky this time. – Scott Ivey Jul 16 '09 at 21:37
  • why is that? you mean like in vista? or in xp it will suddenly stop working? – juan Jul 16 '09 at 21:38
  • 5
    see Scott's comment on Jay Riggs' answer. On W7 and Vista, the Application Data folder is in a different place, so you would need to go up 2 directories, as opposed to 1. – Thomas Jul 16 '09 at 21:43

9 Answers9

208

Try:

System.Environment.GetEnvironmentVariable("USERPROFILE");

Edit:

If the version of .NET you are using is 4 or above, you can use the Environment.SpecialFolder enumeration:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Larry
  • 17,605
  • 9
  • 77
  • 106
Thomas
  • 4,889
  • 4
  • 24
  • 19
  • 20
    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 Jul 16 '09 at 23:21
  • 1
    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 Jul 17 '09 at 15:11
  • The Environment.SpecialFolder is included from .NET 1.1, just doesn't have "UserProfile" folder... – Elad Winkler Jun 23 '14 at 14:23
  • @JimMischel But aren't those supposedly changeable? And if you always reference with the environment variable you're fine. – Nate-Wilkins Sep 29 '14 at 03:00
56

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).ToString();
}

Though using the environment variable is much more clear.

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
Anton Kolesov
  • 7,014
  • 1
  • 17
  • 5
31

You can get the UserProfile path with just this:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

zionyx
  • 1,927
  • 19
  • 14
23

Also very helpful, while investigating the Environment.SpecialFolder enum. Use LINQPad or create a solution and execute this code:

Enum.GetValues(typeof(Environment.SpecialFolder))
    .Cast<Environment.SpecialFolder>()
    .Select(specialFolder => new
    {
        Name = specialFolder.ToString(),
        Path = Environment.GetFolderPath(specialFolder)
    })
    .OrderBy(item => item.Path.ToLower())

Folder Paths

This is the result on my machine:

MyComputer
LocalizedResources
CommonOemLinks
ProgramFiles            C:\Program Files (x86) 
ProgramFilesX86         C:\Program Files (x86) 
CommonProgramFiles      C:\Program Files (x86)\Common Files 
CommonProgramFilesX86   C:\Program Files (x86)\Common Files 
CommonApplicationData   C:\ProgramData 
CommonStartMenu         C:\ProgramData\Microsoft\Windows\Start Menu 
CommonPrograms          C:\ProgramData\Microsoft\Windows\Start Menu\Programs 
CommonAdminTools        C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools 
CommonStartup           C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup 
CommonTemplates         C:\ProgramData\Microsoft\Windows\Templates 
UserProfile             C:\Users\fisch 
LocalApplicationData    C:\Users\fisch\AppData\Local 
CDBurning               C:\Users\fisch\AppData\Local\Microsoft\Windows\Burn\Burn 
History                 C:\Users\fisch\AppData\Local\Microsoft\Windows\History 
InternetCache           C:\Users\fisch\AppData\Local\Microsoft\Windows\INetCache 
Cookies                 C:\Users\fisch\AppData\Local\Microsoft\Windows\INetCookies 
ApplicationData         C:\Users\fisch\AppData\Roaming 
NetworkShortcuts        C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Network Shortcuts 
PrinterShortcuts        C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Printer Shortcuts 
Recent                  C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Recent 
SendTo                  C:\Users\fisch\AppData\Roaming\Microsoft\Windows\SendTo 
StartMenu               C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu 
Programs                C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs 
AdminTools              C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools 
Startup                 C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 
Templates               C:\Users\fisch\AppData\Roaming\Microsoft\Windows\Templates 
Desktop                 C:\Users\fisch\Desktop 
DesktopDirectory        C:\Users\fisch\Desktop 
Favorites               C:\Users\fisch\Favorites 
MyMusic                 C:\Users\fisch\Music 
MyDocuments             C:\Users\fisch\OneDrive\Documents 
MyDocuments             C:\Users\fisch\OneDrive\Documents 
MyPictures              C:\Users\fisch\OneDrive\Pictures 
MyVideos                C:\Users\fisch\Videos 
CommonDesktopDirectory  C:\Users\Public\Desktop 
CommonDocuments         C:\Users\Public\Documents 
CommonMusic             C:\Users\Public\Music 
CommonPictures          C:\Users\Public\Pictures 
CommonVideos            C:\Users\Public\Videos 
Windows                 C:\Windows 
Fonts                   C:\Windows\Fonts 
Resources               C:\Windows\resources 
System                  C:\Windows\system32 
SystemX86               C:\Windows\SysWoW64 

("fisch" is the first 5 letters of my last name. This is the user name assigned when signing in with a Microsoft Account.)

bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • I don't know how I've never heard of LINQPad, tried it, this thing is great, thanks for recommending it. – TravisO Jan 11 '17 at 13:53
  • Now it can also me checked in Visual Studio's C# Interactive, but need to add code at the end: `.ToList().ForEach(item => Console.WriteLine(item))` - to get pretty output. – realsonic Jul 27 '22 at 14:25
4
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
  • 40,768
  • 21
  • 80
  • 118
3

Try:

System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName/
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • 1
    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 Jul 16 '09 at 21:31
  • That returns "C:\Users\Fredrik\AppData" on my Vista machine (the ApplicationData folder is "C:\Users\Fredrik\AppData\Roaming") – Fredrik Mörk Jul 16 '09 at 21:32
2

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
  • 97,548
  • 12
  • 110
  • 164
-3
$env:USERPROFILE = "C:\\Documents and Settings\\[USER]\\"
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
Jeff
  • 9
-4

you can use the following code:

if(Platform.Equals("WinCE"))
{
    m_CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
}
else if(Platform.Equals("Win32NT"))
{
    m_CurrentPath = Directory.GetCurrentDirectory();
}

more information see: Get Current Directory Path in both WinXP and WinCE with C#

drneel
  • 2,887
  • 5
  • 30
  • 48
ksblog
  • 17
  • 1
  • 3
    This is completely out of place here. The OP is asking for the current _user_ folder, not the working folder. – julealgon May 29 '14 at 19:50