.net – Visual Studio Settings file – how does it work

netsettingsvisual studiovisual-studio-2008

I do not understand the following things:

  1. What is the difference between app.config (applicationname.exe.config) and settings file ?
  2. I am unable to locate the *.settings file in Windows7 AppData directory under the specific account (I heard that it should be located somewhere over there) ?
  3. Let's assume that *.settings file is somewhere on the hard drive. Why on the development time the data from settings file are copied to application config file?

Kind Regards
PK

Best Answer

So the default settings are stored in the <exename>.config file located in the same directory as the executable.

The reason you do cannot find the user.config file is it is only created if you have made changes to the defaults. The user settings override the defaults which are stored in the <exename>.config file. If no changes have been made, then no user configuration file will exist. And... once it does exist you will notice that only the settings which have been changes will show up in the user.config file.

To test this... create a dummy variable called like temp in your Settings file. When you start your application do:

Settings1.Default.temp = Settings1.Default.temp + 1;
Settings1.Default.Save();

You will now have a file called user.config created in the user's ApplicationData folder which on Vista is in: C:\Users\<username>\AppData\Local\<company>\<productname>

Here is some code I wrote to help identify where all the various SpecialFolders where on different Operating Systems. (Might want to do a find-replace for log.Debug and replace with Console.WriteLine)

log.Debug("SpecialFolder.ApplicationData: " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData ));
log.Debug("SpecialFolder.CommonApplicationData: " + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData ));
log.Debug("SpecialFolder.ProgramFiles: " + Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
log.Debug("SpecialFolder.CommonProgramFiles: " + Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles ));
log.Debug("SpecialFolder.DesktopDirectory: " + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory ));
log.Debug("SpecialFolder.LocalApplicationData: " + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData ));
log.Debug("SpecialFolder.MyDocuments: " + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments ));
log.Debug("SpecialFolder.System: " + Environment.GetFolderPath(Environment.SpecialFolder.System ));

Output On Windows Server 2003:

SpecialFolder.ApplicationData: "C:\Documents and Settings\blake\Application Data"
SpecialFolder.CommonApplicationData: "C:\Documents and Settings\All Users\Application Data"
SpecialFolder.ProgramFiles: "C:\Program Files"
SpecialFolder.CommonProgramFiles: "C:\Program Files\Common Files"
SpecialFolder.DesktopDirectory: "C:\Documents and Settings\blake\Desktop"
SpecialFolder.LocalApplicationData: "C:\Documents and Settings\blake\Local Settings\Application Data"
SpecialFolder.MyDocuments: "C:\Documents and Settings\blake\My Documents"
SpecialFolder.System: "C:\WINDOWS\system32"

Output on Vista:

SpecialFolder.ApplicationData: "C:\Users\blake\AppData\Roaming"
SpecialFolder.CommonApplicationData: "C:\ProgramData"
SpecialFolder.ProgramFiles: "C:\Program Files"
SpecialFolder.CommonProgramFiles: "C:\Program Files\Common Files"
SpecialFolder.DesktopDirectory: "C:\Users\blake\Desktop"
SpecialFolder.LocalApplicationData: "C:\Users\blake\AppData\Local"
SpecialFolder.MyDocuments: "C:\Users\blake\Documents"
SpecialFolder.System: "C:\Windows\system32"