app.config读取问题 [英] app.config reading problem
本文介绍了app.config读取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用app.config文件
Hi,
I am using an app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="Hi"/>
</appSettings>
</configuration>
在一个类库项目中,我想访问它的值.
我正在使用
and in a class library project I want to access the value of it.
I am using
string str = new System.Configuration.AppSettingsReader().GetValue("test", typeof(System.String)).ToString();
但是appsettings
部分中没有给出该测试的信息.
任何人都可以猜测为什么会发生这种情况.
But it giving that test is not there in the appsettings
section.
Can any one guess why it is happening.
推荐答案
您不能在Class-Library Project中使用App.Config
. Class-Library Project可以引用该项目的App.Config
/Web.Config
文件(Windows/Web等),并在其中添加作为参考.
您可以将appSettings
的key
保留在Final/Output(Windows/Web等)项目的App.Config
/Web.Config
文件中,然后进行访问.
已更新-
下面的链接对此主题进行了讨论,当然,除了引用已引用Class-Library项目的Project的App.Config/Web.config设置外,它当然没有提供任何具体的解决方案.
http://social.msdn.microsoft.com/论坛/zh-CN/csharpgeneral/thread/bbfb03bc-9b1d-4cc7-8fff-c484bb1b93f3
编辑-查看有关该主题的另一个讨论线程. [拼写更正.]
http://stackoverflow.com/questions/435261/asp-net-solution- with-class-library-project
You cannot useApp.Config
in Class-Library Project. Class-Library Project can refer toApp.Config
/Web.Config
file of the Project(Windows/Web etc.) in which it is added as a reference.
You may keep thatkey
ofappSettings
in theApp.Config
/Web.Config
file of your Final/Output(Windows/Web etc.) Project, and then access it.
Updated -
Below link has a discussion on this topic, which certainly does not provide any concrete solution other than referencing App.Config/Web.config settings of the Project in which Class-Library project has been referenced.
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/bbfb03bc-9b1d-4cc7-8fff-c484bb1b93f3
Edit - Have a look at one more discussion thread on this topic. [Spelling corrections.]
http://stackoverflow.com/questions/435261/asp-net-solution-with-class-library-project
试试这个
try this
string str = ConfigurationManager.AppSettings["test"].ToString();
这篇关于app.config读取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文