2013-07-01 76 views
1

我正在爲我們構建的Web應用程序創建一個設置過程。不,我有一個配置文件,它看起來像這樣的,其中包含了部分「的appSettings」,和一節「的applicationSettings」:現在在設置配置文件中訪問section'applicationSettings'(而不是'appSettings')

<configuration> 
<appSettings> 
    <add key="Password" value="dummy"/> 
    <add key="Username" value="dummy"/> 
    <add key="DB" value="dummy"/> 
    <add key="DBServer" value="dummy"/> 
    <add key="LogStoredProcedure" value="dummy"/> 
    <add key="ErrorStoredProcedure" value="dummy"/> 
    <add key="ErrorFileName" value="dummy"/> 
    <add key="EncryptionKey" value="dummy"/> 
</appSettings> 
<applicationSettings> 
    <inoBIBooks.My.MySettings> 
     <setting name="BIDB_Username" serializeAs="String"> 
     <value>Username</value> 
     </setting> 
     <setting name="BIDB_Server" serializeAs="String"> 
     <value>Servername</value> 
     </setting> 
     <setting name="BIDB_Database" serializeAs="String"> 
     <value>Database</value> 
     </setting> 
     <setting name="BIDB_Password" serializeAs="String"> 
     <value>Password</value> 
     </setting> 
    </inoBIBooks.My.MySettings> 
</applicationSettings> 
</configuration> 

,從我的設置我都開從配置文件文件系統與 配置配置= WebConfigurationManager.OpenWebConfiguration(「/」+ targetvdir); 其中變量'targetvdir'包含配置文件的路徑。

我該得到的配置文件,我能夠通過

config.AppSettings.Settings["Password"].Value = "something"; 

編輯「的appSettings」部分,但我不能這樣做,反正用「的applicationSettings」部分。在網絡應用程序本身,我通過訪問該部分

Properties.Settings.Default.<Setting> 

但這不會從我的安裝項目工作。

是否有機會像'appSettings'一節那樣簡單地編輯'applicationSettings'部分?或者我必須編輯xml本身? 任何提示是非常感謝。

親切的問候, 凱哈特曼

回答

0

我通過自己回答我的問題道歉,因爲我找到了解決辦法發佈後立即。這個問題基本上給出了答案:Save and reload app.config(applicationSettings) at runtime

我不得不使用此代碼,寫入到該節「的applicationSettings」:

// this gets the applicationSettings section (and the inner section 'inoBIBooks.My.MySettings') 
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir); 
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings"); 
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["inoBIBooks.My.MySettings"]; 
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection; 

// set a value to that specific property 
SettingElement applicationSetting = clientSection.Settings.Get("BIDB_Username"); 
applicationSetting.Value.ValueXml.InnerText = "username"; 

// without this, saving won't work 
applicationConfigSection.SectionInformation.ForceSave = true; 
// save 
config.Save(); 
相關問題