2011-07-07 33 views

回答

7

您必須直接引用您要覆蓋的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> 
+0

'appname'從哪裏來? –

-1

只能通過編碼:

例如,

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

但是,如果您的問題是在不同的環境中維護不同的配置值,那麼我會使用AppSettings代替。

然後,您可以使用開發人員覆蓋文件。

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

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

+0

的OP被詢問Settings.settings而不是'ApplicationSettings'部。 –

+0

要點 - 修改後! – BonyT

0

這取決於的設置範圍。如果它的應用程序範圍設置在app.config中更改它的值就足夠了。

但是,如果它的用戶範圍設置,則app.config中存在的值只是用於新用戶的默認值,並且已經使用該應用程序的每個用戶都將當前使用的值存儲在單獨的文件中, .config,所以更改app.config中的值對已經運行該應用程序的用戶沒有任何影響。

由於此更改用戶範圍設置的值可能是一個麻煩的任務。您可以檢查以下的答案的詳細信息,更改用戶範圍設置:

Changing User Scope Application Setting

0

使用不同的配置文件進行生產和爲您服務。基本上,你可以在RELEASE中進行編譯,所以如果你使用Visual Studio進行編譯,使用後期構建事件來複制RELEASE配置文件,以防你準備一個生產版本。

我願意這樣的,而不是從代碼進行更改,爲別人是很容易看到在配置文件中differenc,然後去深入到代碼中查找所有if/else東西。

1

對於應用程序範圍連接字符串值:

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