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%");
Best Answer
You are looking for
Environment.SpecialFolder.UserProfile
which refers toC:\Users\myname
on Windows and/home/myname
on Unix/Linux:Note that
Environment.SpecialFolder.Personal
is My Documents (or Documents in win7 and above), but same as home directory on Unix/Linux.