Configuration.GetSection returns 文件中不存在的 ConfigurationSection
Configuration.GetSection returns a ConfigurationSection that doesn't exists in the file
我正在使用包含如下行的 .NET Framework 应用程序:
var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Console.WriteLine(configuration.FilePath);
var mySettings = (MySettings) configuration.GetSection("MySettings");
它打印以下路径:
C:\Users\Daniel Jonsson\AppData\Local\MyCompany\MyApp.exe_Url_ib5gv4f20ng5kujpcanm3fusbnakfvor.0.0.0\user.config
但是,该文件不包含 MySettings
部分。相反,正在读取的 MySettings
部分位于 C:\code\MyCompany\MyApp\bin\Debug\MyApp.exe.config
.
中程序的 .config 文件中
控制此行为的配置文件之间是否存在某种继承关系?或者为什么它会这样?我希望它读取 user.config
文件中的任何内容。
显然 Configuration
class 表示所有配置文件的配置设置的合并视图,根据此 source:
Configuration settings are stored in a hierarchy of configuration files. The Configuration class instance represents the merged view of the configuration settings from all of the configuration files that apply to a specific physical entity, such as a computer, or to a logical entity, such as an application or a Web site. The logical entity can exist on the local computer or on a remote server. For information about configuration files, see Configuring Apps and ASP.NET Configuration Files.
层级如下,据此source:
Machine.config
> [AppName.exe.config]
> Roaming.config
> User.config
当使用 ConfigurationUserLevel.PerUserRoamingAndLocal
时,它会合并所有这些级别,根据前面 source 的这一部分:
Remember that configuration is hierarchical and merged. When requesting roaming or local user configuration, that level up through machine.config are merged, resulting in the complete configuration accessible by your application for the given user level. An interesting consequence of this merging allows you to request roaming or local user configuration even when the User.config file does not exist. The Configuration object returned will contain a FilePath that does not exist and the HasFile property will be false. Any sections defined in higher configuration levels will still be accessible though and with the proper allowances, changes to those sections can be saved. Saving changes at a level that previously did not exist will create the appropriate User.config file.
我正在使用包含如下行的 .NET Framework 应用程序:
var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Console.WriteLine(configuration.FilePath);
var mySettings = (MySettings) configuration.GetSection("MySettings");
它打印以下路径:
C:\Users\Daniel Jonsson\AppData\Local\MyCompany\MyApp.exe_Url_ib5gv4f20ng5kujpcanm3fusbnakfvor.0.0.0\user.config
但是,该文件不包含 MySettings
部分。相反,正在读取的 MySettings
部分位于 C:\code\MyCompany\MyApp\bin\Debug\MyApp.exe.config
.
控制此行为的配置文件之间是否存在某种继承关系?或者为什么它会这样?我希望它读取 user.config
文件中的任何内容。
显然 Configuration
class 表示所有配置文件的配置设置的合并视图,根据此 source:
Configuration settings are stored in a hierarchy of configuration files. The Configuration class instance represents the merged view of the configuration settings from all of the configuration files that apply to a specific physical entity, such as a computer, or to a logical entity, such as an application or a Web site. The logical entity can exist on the local computer or on a remote server. For information about configuration files, see Configuring Apps and ASP.NET Configuration Files.
层级如下,据此source:
Machine.config
>[AppName.exe.config]
>Roaming.config
>User.config
当使用 ConfigurationUserLevel.PerUserRoamingAndLocal
时,它会合并所有这些级别,根据前面 source 的这一部分:
Remember that configuration is hierarchical and merged. When requesting roaming or local user configuration, that level up through machine.config are merged, resulting in the complete configuration accessible by your application for the given user level. An interesting consequence of this merging allows you to request roaming or local user configuration even when the User.config file does not exist. The Configuration object returned will contain a FilePath that does not exist and the HasFile property will be false. Any sections defined in higher configuration levels will still be accessible though and with the proper allowances, changes to those sections can be saved. Saving changes at a level that previously did not exist will create the appropriate User.config file.