Getting the path of the home directory in C#?

C#.Net

C# Problem Overview


Okay, I've checked Environment.SpecialFolder, but there's nothing in there for this.

I want to get the home directory of the current user in C#. (e.g. c:\documents and settings\user under XP, c:\users\user under Vista, and /home/user under Unix.)

I know I can read enviroment variables to find this out, but I want to do this in a cross-platform way.

Is there any way I can do this with .NET (preferably using mscorlib)?

UPDATE: Okay, this is the code I ended up using:

string homePath = (Environment.OSVersion.Platform == PlatformID.Unix || 
                   Environment.OSVersion.Platform == PlatformID.MacOSX)
    ? Environment.GetEnvironmentVariable("HOME")
    : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");

C# Solutions


Solution 1 - C#

You are looking for Environment.SpecialFolder.UserProfile which refers to C:\Users\myname on Windows and /home/myname on Unix/Linux:

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

Note that Environment.SpecialFolder.Personal is My Documents (or Documents in win7 and above), but same as home directory on Unix/Linux.

Solution 2 - C#

Environment.SpecialFolder.Personal doesn't actually return the home folder, it returns the My Documents folder. The safest way to get the home folder on Win32 is to read %HOMEDRIVE%%HOMEPATH%. Reading environment variables is actually very portable to do (across Unix and Windows), so I'm not sure why the poster wanted to not do it.

Edited to add: For crossplatform (Windows/Unix) C#, I'd read $HOME on Unix and OSX and %HOMEDRIVE%%HOMEPATH% on Windows.

Solution 3 - C#

I believe what you are looking for is:

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

For reference, it is infact contained in mscorlib.

Solution 4 - C#

In DotNetCore 1.1 System.Environment.SpecialFolder does not exist. It might exist in 2.0-beta. Until then, to do this you can use the following:

var envHome = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "HOMEPATH" : "HOME";
var home = Environment.GetEnvironmentVariable(envHome);`

Solution 5 - C#

The bottom line answer is No. The is no simple System based method in .NET to get the Home directory such that we could expect an implementation in both .NET on Windows and in Mono.

You will need to do some OS detection and branch to OS specific code.

Solution 6 - C#

When you say cross-platform way, what other OSs do you need to support? You may need to do some simple OS detection to select the method for finding the home directory if you're running on a non-Windows OS.

This website seems to give a way to do what you need in Windows.

Solution 7 - C#

This can be done using GetEnvironmentVariable in System.IO:

public string GetUserHome() {
    var homeDrive = Environment.GetEnvironmentVariable("HOMEDRIVE");
    if (!string.IsNullOrWhiteSpace(homeDrive))
    {
        var homePath = Environment.GetEnvironmentVariable("HOMEPATH");
        if (!string.IsNullOrWhiteSpace(homePath))
        {            
            var fullHomePath = homeDrive + Path.DirectorySeparatorChar + homePath;
            return Path.Combine(fullHomePath, "myFolder");
        }
        else
        {
            throw new Exception("Environment variable error, there is no 'HOMEPATH'");
        }
    }
    else
    {
        throw new Exception("Environment variable error, there is no 'HOMEDRIVE'");
    }
}

Then it produces under windows: C:\\\\Users\\myusername\\myFolder

Note that if you use

var fullHomePath = Path.Combine(homeDrive.ToString(), homePath.ToString())

it fails because it produces: \\Users\\myusername\\myFolder

Solution 8 - C#

I don't have a machine to test it on, but %HOMESHARE% might work for you. Otherwise, here's a pretty good list of environment variables.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMiffTheFoxView Question on Stackoverflow
Solution 1 - C#JamesView Answer on Stackoverflow
Solution 2 - C#sigintView Answer on Stackoverflow
Solution 3 - C#Matthew ScharleyView Answer on Stackoverflow
Solution 4 - C#KBPView Answer on Stackoverflow
Solution 5 - C#AnthonyWJonesView Answer on Stackoverflow
Solution 6 - C#John D.View Answer on Stackoverflow
Solution 7 - C#NicosmikView Answer on Stackoverflow
Solution 8 - C#JP AliotoView Answer on Stackoverflow