views:

407

answers:

2

I've got a gtk file chooser button on my application I am writing in c# using Mono Develop. I would like to set the file chooser's default location to the users' home directory regardless of what user is running it.

I've tried the ~/ short cut - fchFolder1.SetCurrentFolder("~/"); - but this did not work. I was just wondering if there was a value that the gtk file chooser used to refer to the users home directory? Thanks

A: 

In C, one would use g_get_home_dir() to find the user's home directory, and set the file chooser's current location to that, but as far as I can tell, that function isn't wrapped in GTK#. Someone asked the same question on the GTK# mailing list and the answer was to use

System.Environment.GetFolderPath (SpecialFolder.Personal)
ptomato
A: 

In Unix, you can either get the HOME environment variable or use System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal).

In Windows, expand %HOMEDRIVE%%HOMEPATH% to get the "home" directory, or use the same GetFolderPath call to get the "My Documents" directory.

Discussion about the HOME and HOMEDRIVE+HOMEPATH approach: http://stackoverflow.com/questions/1143706/getting-the-path-of-the-home-directory-in-c

Johannes Sasongko
Thanks I used the code on the link to the discussion you posted :) just in case any one else has the same problem I used the following code to make it cross platform:string homePath = (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) ? Environment.GetEnvironmentVariable("HOME") : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
Connel