从类库项目中的App.config中读取 [英] Read from App.config in a Class Library project

查看:164
本文介绍了从类库项目中的App.config中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的类库项目,该项目将为我提供一个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的想法是正确的吗? / p>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆