次のようなapp.configファイルがあります。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TestKey" value="TestValue" />
</appSettings>
<newSection>
</newSection>
</configuration>
そして、私はそれをこのように使おうとしています:
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(@"C:\app.config");
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
ただし、機能していないようです。ファイルが読み込まれた直後に中断してデバッグし、確認しようとすると、が表示されconfiguration.AppSettings
ます'configuration.AppSettings' threw an exception of type 'System.InvalidCastException'
。
configuration.Sections ["newSection"]を見ると、{System.Configuration.DefaultSection}
(nullではなく)空が返されるため、ファイルを読んでいると確信しています。
非常に基本的な問題があると思います...AppSettingsで何が起こっているのですか?