如何覆盖设置。通过app.config变量设置变量

时间:2021-10-05 23:13:36

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

如何更改(或覆盖)设置。通过在生产环境中向app.config添加一个变量来设置变量?

Is this possible anyway?

这是可能的呢?

5 个解决方案

#1


7  

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

您必须直接引用要重写的applicationSettings,并显式指定具有替换值的属性。

<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


1  

For an application scope connection string value:

对于应用程序范围连接字符串值:

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

#3


0  

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

这取决于设置的范围。如果应用程序范围设置在app.config中更改其值就足够了。

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.

但是,如果它是一个用户范围设置,那么application .config中的值只是新用户的默认值,每个已经使用该应用程序的用户都将当前使用的值存储在一个单独的文件中,即user。配置,因此改变app.config中的值对已经运行应用程序的用户没有任何影响。

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


0  

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.

为生产和您使用不同的配置文件。基本上,您将在发行版中编译,因此,如果您使用Visual Studio,则使用post构建事件来复制发布配置文件,以防您为生产准备了一个构建版本。

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.

我宁愿这样而不是从代码中修改它,因为对于其他人来说,在配置文件中看到差异要容易得多,然后深入到代码中查找所有if/else内容。

#5


-1  

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.

但是,如果您的问题是在不同的环境中维护不同的配置值,那么我将使用AppSettings。

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

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

#1


7  

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

您必须直接引用要重写的applicationSettings,并显式指定具有替换值的属性。

<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


1  

For an application scope connection string value:

对于应用程序范围连接字符串值:

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

#3


0  

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

这取决于设置的范围。如果应用程序范围设置在app.config中更改其值就足够了。

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.

但是,如果它是一个用户范围设置,那么application .config中的值只是新用户的默认值,每个已经使用该应用程序的用户都将当前使用的值存储在一个单独的文件中,即user。配置,因此改变app.config中的值对已经运行应用程序的用户没有任何影响。

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


0  

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.

为生产和您使用不同的配置文件。基本上,您将在发行版中编译,因此,如果您使用Visual Studio,则使用post构建事件来复制发布配置文件,以防您为生产准备了一个构建版本。

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.

我宁愿这样而不是从代码中修改它,因为对于其他人来说,在配置文件中看到差异要容易得多,然后深入到代码中查找所有if/else内容。

#5


-1  

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.

但是,如果您的问题是在不同的环境中维护不同的配置值,那么我将使用AppSettings。

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

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