How to override settings.settings variable by app.config variable

非 Y 不嫁゛ 提交于 2019-12-10 14:45:58

问题


How can I change (or override) a settings.settings variable by adding a variable to the app.config on production?

Is this possible anyway?


回答1:


You have to directly reference the applicationSettings you're trying to override and explicitly specify the property that has a replaced value.

<configuration>
  <!-- section definitions for all elements in <configuration> tag -->
  <configSections>
    <!-- section group, meaning: there will be a <applicationSettings> tag in you configuration-->
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <!-- defines that there will be a <appname.Properties.Settings> tag inside your <applicationSettings> tag -->
      <section name="appname.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <applicationSettings>
    <appname.Properties.Settings>
      <!-- name of the property you want to override -->
      <setting name="setting1" serializeAs="String">
        <!-- new value -->
        <value>new string value</value>
      </setting>
    </appname.Properties.Settings>
  </applicationSettings>
</configuration>



回答2:


For an application scope connection string value:

  <connectionStrings>
    <add name="appname.Properties.Settings.setting1" connectionString="test string" providerName="dbProvider"/>
  </connectionStrings>



回答3:


It depends on the scope of the settings. If its an application scope setting changing its value in app.config is sufficient.

However, if its a user scope setting then the value present in app.config is just the default used to new users and every user that already used the application will have the currently used value stored in a separate file, the user.config, so changing the value in app.config will have no effect to users that already run the application once.

Due to this changing the value of an user scope setting can be a troublesome task. You can check the following answer for more information on changing a user scope setting:

Changing User Scope Application Setting




回答4:


Use different config files for production and for you. Basically on production you would compile in RELEASE, so if you use Visual Studio for it, use post build events to copy RELEASE config file in case you prepare a build for production.

I would prefer this instead of changing it from the code, as for someone else is much easier to see the differenc in config file, then going deep into the code to find all the if/else stuff.




回答5:


Only through code:

e.g.

 if (bool.Parse(ConfigurationManager.AppSettings["overridethis"].ToString()))
 {
     //use overridden value
 }

If however, your issue is maintaining different configuration values in different environments, then I would use AppSettings instead.

You can then use a developer override file.

 <appSettings file="..\user.config">

See http://www.compiledthoughts.com/2005/03/overriding-webconfig-app-settings-with.html



来源:https://stackoverflow.com/questions/6608240/how-to-override-settings-settings-variable-by-app-config-variable

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