从类库中访问app.config文件 [英] access app.config file from class library

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

问题描述

您好,

我在Visual Studio解决方案中有一个类库,我在

类库中添加了app.confing文件

 < span class =code-keyword><   configuration  >  
<跨度类= 代码关键字>< <跨度类= 代码leadattribute>的appSettings <跨度类= 代码关键字>>
< add key = ConnectionString value = Server = ServerName; Database = DbNAme; UID = uid; password = password / >
< / appSettings >
< / configuration >



现在我想要访问来自类库的ConnectionString,如何访问它

(用于类库这个不工作 - ConfigurationManager.AppSettings [ 的ConnectionString])?

解决方案

为什么不将应用程序的设置对象传递给类库中的类?


我会猜测它不能从类库中访问,因为类库没有引用System.configuration组件。确保您的类库引用它。



此外,根据您使用的.Net版本,您可以直接将连接字符串添加到配置文件的connectionStrings元素,并使用ConfigurationManager访问它。的ConnectionStrings [ 的connectionStringName]的ConnectionString而不是陈旧ConfigurationSettings.AppSettings方法

检查这些博客

http://www.pcreview.co.uk/forums/read-application-config-file-class-library -t3830284.html [ ^ ]

http://stackoverflow.com/questions/1318423/access-app-config-settings-from-class-library-called-through-unit-test-project [ ^ ]

--NDK


Hello,
I have a class library in Visual studio solution, i have added app.confing file in
class library

<configuration>
  <appSettings>
    <add key="ConnectionString" value="Server=ServerName; Database=DbNAme; UID=uid; password=password"/>
  </appSettings>
</configuration>


now i want to access ConnectionString from class library, how can i access it?
(this doesnt work for class library - ConfigurationManager.AppSettings["ConnectionString"];)

解决方案

Why don't you pass your app's Settings object to the class in the class library?


I would guess it's not accessible from your class library as the class library doesn't reference the System.configuration component. Make sure this is referenced by your class library.

Also, depending on the version of .Net you are using, you can add a connection string directly to the connectionStrings element of your config file and access it using ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString rather than the obsolete ConfigurationSettings.AppSettings method.


check these blogs
http://www.pcreview.co.uk/forums/read-application-config-file-class-library-t3830284.html[^]
http://stackoverflow.com/questions/1318423/access-app-config-settings-from-class-library-called-through-unit-test-project[^]
--NDK


这篇关于从类库中访问app.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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