日期:2014-05-18  浏览次数:21022 次

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