views:

143

answers:

3

In .NET, how should I get access to a folder for holding configuration data specific to the current machine or user (to avoid hard-coding a path)?

(related:) http://stackoverflow.com/questions/1152065/which-is-the-best-location-to-keep-program-configuration-file-in-windows

Question answered... by the way, here's typical WinXP output of

foreach (Environment.SpecialFolder f in 
         Enum.GetValues(typeof(Environment.SpecialFolder)))
    Debug.WriteLine(string.Format("{0,16}: {1}", 
                    f.ToString(), Environment.GetFolderPath(f)));
         Desktop: C:\Documents and Settings\user\Desktop
        Programs: C:\Documents and Settings\user\Start Menu\Programs
        Personal: C:\Documents and Settings\user\My Documents
        Personal: C:\Documents and Settings\user\My Documents
       Favorites: C:\Documents and Settings\user\Favorites
         Startup: C:\Documents and Settings\user\Start Menu\Programs\Startup
          Recent: C:\Documents and Settings\user\Recent
          SendTo: C:\Documents and Settings\user\SendTo
       StartMenu: C:\Documents and Settings\user\Start Menu
         MyMusic: C:\Documents and Settings\user\My Documents\My Music
DesktopDirectory: C:\Documents and Settings\user\Desktop
      MyComputer: 
       Templates: C:\Documents and Settings\user\Templates
 ApplicationData: C:\Documents and Settings\user\Application Data
LocalApplicationData: C:\Documents and Settings\user\Local Settings\Application Data
   InternetCache: C:\Documents and Settings\user\Local Settings\Temporary Internet Files
         Cookies: C:\Documents and Settings\user\Cookies
         History: C:\Documents and Settings\user\Local Settings\History
CommonApplicationData: C:\Documents and Settings\All Users.WINDOWS\Application Data
          System: C:\WINDOWS\system32
    ProgramFiles: C:\Program Files
      MyPictures: C:\Documents and Settings\user\My Documents\My Pictures
CommonProgramFiles: C:\Program Files\Common Files
+1  A: 

Except for the App.config, that has to be in the application folder, you can put them anywhere. I would recommend however that you establish a logical organized folder structure under your application folder for all the required application files, and a "config" folder for all config files is certainly a good start at that.

Just remember that if a config file is not in the application folder, you have to put a redirection line in the app.config to tell the configuration subsystem where to find it...

 <appSettings configSource="Config\AppSettings.config" />
Charles Bretana
Under the application folder? I heard Program Files is read-only on Vista.
Qwertie
+1  A: 

string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

Look here for all possible values of SpecialFolder

codymanix
+1  A: 

You can use Environment.GetFolderPath() to get a path to special folders on the system, regardless of their location, including the standard locations for config folders and files to be kept:

// Returns the user specific config folder
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
// Returns the computer specific config folder
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

You can then append a string to your specific application folder and save your data there.

Nidonocu
This is a good example. Especially in cases when you may be deploying a ClickOnce application.
BozoJoe