2014-02-22 94 views
0

我在Visual Studio 2010中工作。我在解决方案中有一个C#项目,并在主项目目录中有一个App.config。我试图从Main()方法App.config中读取属性,如下所示:读取C#项目中的App.config元素

String test = ConfigurationManager.AppSettings["streamType"]; 

我App.config文件看起来是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" 
       type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
     <section name="InteropClient.Properties.Settings" 
       type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
       requirePermission="false" /> 
    </sectionGroup> 
    <section name="connections" type="" /> 
    <section name="streamType" type="" /> 
    <section name="messageClass" type="" /> 
    <section name="serializationMethod" type="" /> 
    <section name="encryptionMethod" type="" /> 
    </configSections> 

    <connections> 
    <connection type="zkClient" connectionString="168.72.70.62:9181" sessionTimeout="5000" initInstructions="" name="zk1" 
       classes="Sodao.Zookeeper.Config.ZookeeperConfig, Zookeeper" /> 
    <connection type="TcpClient" connectionString="" initInstructions="" /> 
    </connections> 
    <streamType>MemoryStream</streamType> 
    <messageClass>XGenericMessage</messageClass> 
    <serializationMethod>Thrift</serializationMethod> 
    <encryptionMethod></encryptionMethod> 
</configuration> 

尝试运行的Main()方法,上面的C#行会抛出ConfigurationErrorsException

配置系统初始化失败。

我不确定这是为什么。我想我已经在指示中做了所有事情。我很感谢在这个问题上的一些澄清。

回答

1

我相信它应该看起来像这样。

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    </configSections> 
    **<appSettings> 
     <add key="streamType" value="Somevalue" /> 
    </appSettings>** 
</configuration> 

您的AppSettings节点位于configSections之外。

+0

谢谢,明白了。 –

0

这是你将如何使用的AppSettings:

<appSettings> 
    <add key="streamType" value="Somevalue" /> 
</appSettings> 
+0

我试过了,仍然遇到同样的问题。 –

0

您没有设置你想要的部分的类型..

<section name="streamType" type="" /> 

你应该增加这部分的初始化

+0

仍然不起作用。同样的错误。 –

+0

你填在那里?用类型=“MemoryStream”填充它的 – knightsb

+0

。 –