2012-01-21 16 views
0

私はちょうど単純なものが欠けていると思います。私はexe.configファイルのセクションを読み書きする必要があります。GetSectionオブジェクトをIDictionaryにキャストすることはできません<>

var appConfiguration = ConfigurationManager.OpenExeConfiguration("Mytest.Console.exe"); 
var fileEnvironment = appConfiguration.GetSection("fileEnvironment"); 

、これが私のapp.configをである:私は私のコードでこれを持って

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
     <section name="fileEnvironment" type="System.Configuration.DictionarySectionHandler"/> 
    </configSections> 

    <fileEnvironment> 
     <add key="TestEntry1" value="A nice value"/> 
     <add key="TestEntry2" value="Another value"/> 
    </fileEnvironment> 
</configuration> 

マイappConfiguration変数は{} System.Configuration.Configurationとして返され、 "HasFile" プロパティが設定されています本当に。

変数 "fileEnvironment"をキャストせずにSystem.Configuration.DefaultSectionとして返されます。 GetSectionメソッドにファイルIDictionary<string, string>を追加すると、fileEnvironmentはnullになります。

アイデア?

+0

GetSectionは、DictionaryではなくConfigurationSectionを返します。したがって、それについては何も変わりません。 fileEnvironment変数で何ができると思われますか? –

+0

この前のquesntionと答えをチェックしましたか... http://stackoverflow.com/questions/1278730/net-configurationmanager-app-config-confusion – Lloyd

+0

はい、私はそれを読んでいます。これがDictionarySectionHandlerについて学んだところです。とにかく、fileEnvironmentセクション全体を辞書として抽出し、その一部を変更し、辞書全体を設定ファイルに保存したいと思います。これは可能ですか? – JimBoone

答えて

0

辞書の問題を研究し続けて、this stackoverflow Q&Aを思いついた!辞書の代わりにコレクションを生成しますが、解決策を指します。みんなありがとう。

関連する問題