1

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:) 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
Community
  • 1
  • 1
Qwertie
  • 16,354
  • 20
  • 105
  • 148

3 Answers3

1

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
  • 143,358
  • 22
  • 150
  • 216
1
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

Look here for all possible values of SpecialFolder

codymanix
  • 28,510
  • 21
  • 92
  • 151
1

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
  • 12,476
  • 7
  • 42
  • 43
  • 1
    This is a good example. Especially in cases when you may be deploying a ClickOnce application. – BozoJoe Oct 07 '09 at 23:53