How can I get the current user directory?

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 for all users? i.e:.

 "C:\\Documents and Settings\\[USER]\\" 
+57
c # windows environment special-folders
Jul 16 '09 at 21:21
source share
9 answers

Maybe this will be a good solution: considering whether it 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(); } 

Although the use of the environment variable is much clearer.

+32
Jul 17 '09 at 7:18
source share

Try:

 System.Environment.GetEnvironmentVariable("USERPROFILE"); 

Edit:

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

 Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); 
+110
Jul 16 '09 at 21:30
source share

You can get the path to UserProfile with just this:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

+12
Mar 06 '13 at 11:07
source share
 Environment.GetEnvironmentVariable("userprofile") 

Attempting to switch on behalf of SpecialFolder is subject to problems. There are many reasons why folders will not be where you expect them - users can move them themselves, the GPO can move them, redirect folders to UNC paths, etc.

Using an environment variable for a user file should reflect any of these potential problems.

+4
Jul 16 '09 at 21:32
source share

Try:

 System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName/ 
+3
Jul 16 '09 at 21:26
source share

Also very useful when exploring the Environment.SpecialFolder enumeration. 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 of my car:

 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 

Btw. "fisch" is the first five letters of my last name (and for "fish" - German). This is the username assigned when logging into the Microsoft account.

+3
Nov 28 '16 at 21:43
source share

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

+1
Jul 18 '09 at 9:03
source share

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

Additional information: Get the current directory path in both WinXP and WinCE with C #

-3
Jan 14 '11 at 2:14
source share
 $env:USERPROFILE = "C:\\Documents and Settings\\[USER]\\" 
-3
Jan 30 '15 at 19:24
source share



All Articles