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

前端 未结 5 1780
攒了一身酷
攒了一身酷 2021-01-18 04:32

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

Is this possible anyway?

相关标签:
5条回答
  • 2021-01-18 04:41

    For an application scope connection string value:

      <connectionStrings>
        <add name="appname.Properties.Settings.setting1" connectionString="test string" providerName="dbProvider"/>
      </connectionStrings>
    
    0 讨论(0)
  • 2021-01-18 04:50

    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

    0 讨论(0)
  • 2021-01-18 04:51

    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

    0 讨论(0)
  • 2021-01-18 04:59

    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>
    
    0 讨论(0)
  • 2021-01-18 05:00

    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.

    0 讨论(0)
提交回复
热议问题