我有一个app.config文件看起来像这样:我的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}
(而不是空)。
我猜我已经有一些非常基本的错误... AppSettings发生了什么?
不幸的是,事实并非如此。我把newSection放在刚才确定我真的在加载文件。删除它没有区别。 – Beska