2016-09-02 6 views
0

自分でクラスを作成せずに簡単なセクションを作成したい、他の投稿を読んで自分のセクションを構築しようとしましたが、System.InvalidCastExceptionの例外がスローされます私のセクションを取得しようとします。誰かが私にそれを解決する方法を教えてもらえますか?ありがとう!app.configのcustomizeセクションを追加した後のSystem.InvalidCastException

例外メッセージ

:入力する「System.Configuration.KeyValueInternalCollection」タイプのオブジェクトをキャストすることができません「システム:

型「System.InvalidCastExceptionの」の未処理の例外がHttpServer.exe

で発生しました追加情報.Configuration.AppSettingsSection 'をクリックします。

App.configファイル:

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

    <extensions> 
    <add key=".gif" value="image/gif"/> 
    <add key=".png" value="image/png"/> 
    <add key=".jpg" value="image/jpeg"/> 
    </extensions> 

    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
</configuration> 

C#コード:

AppSettingsSection section = (AppSettingsSection)ConfigurationManager.GetSection("extensions"); 
Console.WriteLine(section.Settings[".gif"].Value.ToString()); 

答えて

0

変更System.Configuration.NameValueSectionHandlerにSystem.Configuration.AppSettingsSectionと System.Collections.Specializedで値を取得します。 NameValueCollection

関連する問題