首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 类库 读取.config配置文件内容解决思路

2012-02-14 
C# 类库 读取.config配置文件内容我建了一个C#类库然后为该类库建了一个app.config配置文件那我的类库中的

C# 类库 读取.config配置文件内容
我建了一个C#   类库  
然后为该类库建了一个app.config配置文件
那我的类库中的类如何读取这个配置文件中的内容呢?
请达人指教!!
谢谢。

[解决办法]
.config文件
<?xml version= "1.0 " encoding= "utf-8 " ?>

<!-- .NET application configuration file

This file must have the exact same name as your application with
.config appended to it. For example if your application is testApp.exe
then the config file must be testApp.exe.config it mut also be in the
same directory as the application. -->

<configuration>

<appSettings>
<add key= "test " value= "testvalue "/>
</appSettings>

</configuration>

读取:
string read = ConfigurationManager.AppSettings[ "test "];

记得引用System.configuration


[解决办法]
<?xml version= "1.0 " encoding= "utf-8 "?>
<configuration>
<appSettings>
<add key= "connstr " value= "user id=root; password=laoshu; host=localhost; database=ume;charset=gb2312 "/>
</appSettings>
</configuration>
上面是config文件的内容
System.Configuration.ConfigurationSettings.AppSettings[ "connstr "]的值为
user id=root; password=laoshu; host=localhost; database=ume;charset=gb2312

热点排行