在C#项目中读取App.config元素
我在VisualStudio2010工作。我在解决方案中有一个C#项目,在主项目目录中有一个App.config。我正在尝试从在C#项目中读取App.config元素,c#,app-config,configurationmanager,C#,App Config,Configurationmanager,我在VisualStudio2010工作。我在解决方案中有一个C#项目,在主项目目录中有一个App.config。我正在尝试从Main()方法读取App.config中的属性,如下所示: String test = ConfigurationManager.AppSettings["streamType"]; 我的App.config文件如下所示: <?xml version="1.0" encoding="utf-8"?> <configuration> <
Main()
方法读取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>
记忆流
XGenericMessage
节俭
试图运行Main()方法时,上面的C#行抛出一个ConfigurationErrorsException
异常
配置系统初始化失败
我不知道这是为什么。我想我已经完成了说明书中的所有内容。我希望您能澄清这个问题。以下是您使用AppSettings的方式:
<appSettings>
<add key="streamType" value="Somevalue" />
</appSettings>
您没有设置所需节的类型
<section name="streamType" type="" />
您应该为初始化添加此部分我相信它应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
**<appSettings>
<add key="streamType" value="Somevalue" />
</appSettings>**
</configuration>
**
**
您的AppSettings节点位于configSections之外。将其填充为type=“MemoryStream”。