tags:

views:

1860

answers:

6

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%");
+1  A: 

I believe what you are looking for is:

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

For reference, it is infact contained in mscorlib.

Matthew Scharley
That returns the My Documents folder.
MiffTheFox
"My Documents" *is* the closest thing Windows has to a home directory.
MSalters
@MStallers No, %HOMEDRIVE%%HOMEPATH% is.
MiffTheFox
Not quite... but the alternative is PInvoke, and the OP asked for a managed solution.
Matthew Scharley
A: 
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
Philippe Leybaert
That returns the My Documents folder.
MiffTheFox
A: 

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.

John D.
Unless you're using some braindead implementation of the Framework Environment.GetFolder() should work cross-platform.
Martinho Fernandes
*Should*. MS's implementation doesn't do it.
Martinho Fernandes
+7  A: 

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.

sigint
I know for a fact HOMEDRIVE doesn't exist on *nix, and normally it's just HOME, not HOMEPATH.
Matthew Scharley
Matthew, you're right. I added a clarification on the original comment.
sigint
+1 Agreed, this is the closest we're gonna get.
AnthonyWJones
Can you not use %USERPROFILE% on Windows?
Lucas Jones
Mac OSX *is* UNIX, officially. It also works on Linux, which is similar enough to UNIX.
MSalters
$HOME works on OS X too. This still isn't the correct answer as far as I'm concerned though since you're doing platform detection... but to get at the exact values asked for, that's about what you have to do.
Matthew Scharley
Actually, OS X is BSD, but that's a distinction for the anals of some history book or another...
Matthew Scharley
Thank you, I posted the code I ended up using to the original question.
MiffTheFox
person-b, %USERPROFILE% and %HOMEDRIVE%%HOMEPATH% are often the same on personal PCs, but often point to different places in networked environments. E.g., at work my USERPROFILE is C:\Documents and Settings\username but home directory is Z:\, which is a networked drive which is backed up by IT regularly.
sigint
A: 

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.

JP Alioto
+1  A: 

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.

AnthonyWJones