AppSettings に基づいてカスタム構成ファイル セクションを作成しようとしています。
<configSections>
<section name="customConfiguration"
type="System.Configuration.AppSettingsSection,
System.Configuration,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
ConfigurationManager.GetSection("customConfiguration") で読み込もうとしたところ、返されたオブジェクトのタイプは System.Configuration.KeyValueInternalCollection でした。キーは確認できましたが、このコレクションの値を読み取ることができず、AppSettingsSection にキャストできませんでした。
このStackoverflowの回答は、使用する必要があることを示唆しています
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection customSettingSection =
(AppSettingsSection)config.GetSection("customConfiguration");
これはうまくいきました。私の質問は: ConfigurationManager.GetSection() と Configuration.GetSection() の違いは何ですか? 1 つをいつ使用し、もう 1 つをいつ使用する必要がありますか?