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%");
Environment.SpecialFolder.Personaldoesn’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
$HOMEon Unix and OSX and%HOMEDRIVE%%HOMEPATH%on Windows.