从类库项目中的 App.config 读取 [英] Read from App.config in a Class Library project
问题描述
我正在开发一个简单的类库项目,它会给我一个 dll.
I am developing a simple class library project, which will give me a dll.
我想从配置文件中读取一个特定的值.所以我在我的项目中添加了一个 App.config 文件.
I wanted a particular value to be read from a config file. So I have added an App.config file to my project.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="serviceUrl" value="test value" />
</appSettings>
</configuration>
以上是我的 App.config 文件,现在我正在尝试按照以下方式阅读它
Above is my App.config file, and now I am trying to read it as following
string strVal = System.Configuration.ConfigurationManager.AppSettings["serviceUrl"];
但是我的字符串变量没有得到任何值.
But I am not getting any value in my string variable.
我以类似的方式为 Web 应用程序完成了此操作,并且奏效了.但不知何故,我无法让它发挥作用.
I had done this for a web application in a similar way and it worked. But somehow I am not able to get this working.
首先将 App.config 放在类库项目中的想法是否正确?
Is the idea of having App.config in a class library project correct in the first place ?
推荐答案
如我的评论中所述,将 App.Config 文件添加到主解决方案中,而不是在类库项目中.
As stated in my comment, add the App.Config file to the main solution and not in the class library project.
这篇关于从类库项目中的 App.config 读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!