2011-10-25 11 views
0

設定値に依存する機能をテストしようとしています(Settings["foo"] = trueの場合は5、そうでない場合は-1を返します)。tetsプロジェクトのapp.configファイルの設定値を変更する

私がしようとしているのは、実行時に設定値を変更することです。
私の設定ファイルは、そう(簡体字)のようになります。

<configSections> 
     <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">  
     <section name="DomainSettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 
     </sectionGroup> 
    </configSections> 

<applicationSettings> 
<DomainSettings> 
    <setting name="foo" serializeAs="String"> 
     <value>false</value> 
    </setting> 
</ICTS.SmartQueue.Domain.DomainSettings> 
</applicationSettings> 

と私は次のことをやっている:

//get config file 
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
//get relevant section 
var section = (ClientSettingsSection)config.GetSection("applicationSettings/DomainSettings"); 
//get element from section 
var element = section.Settings.Get("Foo"); 
//change its value and save it 
element.Value.ValueXml.InnerText = true.ToString(); 
config.Save(System.Configuration.ConfigurationSaveMode.Modified, true); 
//force refresh 
ConfigurationManager.RefreshSection("applicationSettings/DomainSettings"); 

私はテストのconfigを見たときに値が実際に変更されていることがわかりますファイルを 'Out'ディレクトリ(MyTests.DLL.config)に保存します。
ただし、DomainSettings.Default.Fooは「偽」と評価されます。

答えて

0

設定ファイルがキャッシュされます。つまり、新しい値でconfigファイルを変更するだけで、アプリケーションがリロードされるまでリロードされません。次に、新しい値が変更されたことがわかります。しかし、あなたのコードでconfigファイルを参照すると、ファイルを読み込まず、キャッシュされている設定を読み込みます。あなたの場合は、ではなく、新しい値でが更新されています。

+0

私は、 'ConfigurationManager.RefreshSection'がそれを処理するべきだと考えました。 (MSDNから: "このメソッドは、他のセクションに影響を与えずに指定された構成セクションのキャッシュを無効にします。") –

0

実行時に設定ファイルを変更することができますが、アプリケーションを再起動するまで変更内容が反映されないためです。 ASP.NETは異なっており、Web.configファイルが変更された直後に変更が反映されます。

+0

Web設定の値を変更すると、実行時に変更が即座に表示されますか?自動的にアプリを再起動すると? –

+0

はい、アプリが自動的に再起動されるためです。 – Icarus

関連する問題