.NET 2.0 Configuration配置文件的保存问题
TestSection section = config.Sections["Test"] as TestSection;
section.UserName = "Vivi";
// 在Vistual Studio调试时,config.FilePth是“AppName.vshost.exe.config”;
// 正常运行程序(不是在Visual Studio中按F5启动)时,config.FilePth才是“AppName.exe.config”。
// 所以,这两种不同情况下config.Save();保存到了不同的文件中。
config.Save();
config.SaveAs("myconfig.xml");
// 执行完上面的语句“config.SaveAs("myconfig.xml");”后,config.FilePth变成了“myconfig.xml”,
// 所以下面的config.Save()仍然是保存配置到myconfig.xml,而不是AppName.exe.config。
config.Save();
from:
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_aspnetconfig/html/c282a0c3-0642-458b-8426-565ead922749.htm
Scope of Configuration Settings
Scope of Configuration Settings
Configuration level |
File name |
File description |
Server |
Machine.config |
The Machine.config file contains the ASP.NET schema for all of the Web applications on the server. This file is at the top of the configuration merge hierarchy. |
Root Web |
Web.config |
The Web.config file for the server is stored in the same directory as the Machine.config file and contains default values for most of the system.web configuration sections. At run time, this file is merged second from the top in the configuration hierarchy. |
Web site |
Web.config |
The Web.config file for a specific Web site contains settings that apply to the Web site and inherit downward through all of the ASP.NET applications and subdirectories of the site. |
ASP.NET application root directory |
Web.config |
The Web.config file for a specific ASP.NET application is located in the root directory of the application and contains settings that apply to the Web application and inherit downward through all of the subdirectories in its branch. |
ASP.NET application |
Web.config |
The Web.config file for an application subdirectory contains settings that apply to this subdirectory and inherit downward through all of the subdirectories in its branch. |
Client application |
ApplicationName.config |
The ApplicationName.config file contains settings for a Windows client application (not a Web application). |
Configuration Structure
ASP.NET configuration files are called Web.config files, and they can appear in multiple directories in ASP.NET applications. The ASP.NET configuration hierarchy has the following characteristics:
-
Uses configuration files that apply to the resources within the directory in which the configuration file is placed and to all of its child directories.
-
Enables you to place configuration data where it will have the appropriate scope: the entire computer, all Web applications, an individual application, or a subdirectory within the application.
-
Permits overriding the configuration settings that are inherited from higher levels in the configuration hierarchy. Also permits locking configuration settings to prevent them from being overridden by configuration settings at a lower level.
-
Organizes logical groups of configuration settings into sections.
关于.NET 2.0 Configuration的文章:
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration2.aspx
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration3.aspx