アプリケーションの構成ファイルをプログラムで読み取り/書き込み(および保存)したい
app.configは次のようになります。
<configuration>
<configSections>
<section name="AdWordsApi" type="System.Configuration.DictionarySectionHandler" requirePermission="false"/>
</configSections>
<AdWordsApi>
<add key="LogPath" value=".\Logs\"/>
...
</AdWordsApi>
</configuration>
ConfigurationManager.GetSectionを使用してapp.configを読み取ると、次のように機能します。
var adwords_section = (System.Collections.Hashtable) System.Configuration.ConfigurationManager.GetSection("AdWordsApi");
Console.WriteLine((string)adwords_section["LogPath"]);
しかし、ConfigurationManager.OpenExeConfigurationを使用すると:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ConfigurationSection section = config.GetSection("AdWordsApi");
Console.WriteLine(section["LogPath"]);
私はいつもこのエラーを受け取ります:
'System.Configuration.ConfigurationElement.this [System.Configuration.ConfigurationProperty]'は、保護レベルが原因でアクセスできません
しかし、私が知っているように、GetSectionはプログラムの実行時に構成を保存できません。最初に言ったように、プログラムの実行時に構成を保存したいので、OpenExeConfigurationを使用する必要があります。
私は長い間グーグルで検索してきましたが、AppSettingsを使用することがわかりましたが、使用するのはカスタムセクションです。
この「ConfigurationPropertyにアクセスできません」エラーが発生した理由を誰かが説明できますか?ありがとう
編集:
SystemとSystem.Configurationのローカルコピーをtrueに設定しました