Visual Studio Settings file - how does it work?

戏子无情 提交于 2019-11-30 09:47:48
blak3r

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"
  1. The app.config file represents settings intrinsic to the application, and will apply to all sessions of the application. The *.settings file is typically for settings specific to a given user's use of an application.

  2. Try looking in the Application Data directory beneath Local Settings [«username»\Local Settings\Application Data\«app name ...»], instead of the user's direct Application Data directory [«username»\Application Data].

  3. The copy of the settings in the app.config file for a given *.settings file are used to initialize it the first time those settings are used; including the case when a new setting is added and a prior copy of the *.settings file doesn't have the new setting yet.

Here is a link for a brief walk-through of settings in C#.

Using Settings in C# (MSDN)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!