为什么我读取不到app.config中的配置参数?
我在我的类库工程中新添加了一个app.config文件,可是我读取不到里面的配置参数!!比如说我读取数据库连接字符串:string ConnString = ConfigurationSettings.AppSettings["ConnectionString"];调试进去发现ConnString为null,这是为什么呀??? --------------------编程问答-------------------- 把app.config贴出来 --------------------编程问答-------------------- 贴出你的app.config看看 --------------------编程问答-------------------- <add key="__SysAccount__" value="test" />
<add key="__SysPassword__" value="test" /> --------------------编程问答-------------------- 在app.config的Properties里面, 把它的Build Action改为Embeded Resource --------------------编程问答-------------------- connection字串不是这么读的。试试这个:
ConfigurationManager.ConnectionStrings["test"]
web.config
<connectionStrings>
<add name="test" connectionString="cocoo"/>
</connectionStrings> --------------------编程问答-------------------- 我在读取的时候提示NUll,希望高手帮忙解决一下:
app.config文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString ="server=.;database=test;uid=sa;pwd=sa" providerName="System.Data.SqlClient=" />
</connectionStrings>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="PASSManager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<PASSManager.Properties.Settings>
<setting name="PASSManager_DBWebService_WebService" serializeAs="String">
<value>http://localhost:3459/WebService.asmx</value>
</setting>
</PASSManager.Properties.Settings>
</applicationSettings>
<appSettings>
<add key="ConnectionString" value ="server=.;database=test;uid=sa;pwd=sa"/>
</appSettings>
</configuration>
我的读取方法如下:
string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; --------------------编程问答--------------------
+1 --------------------编程问答-------------------- 你说你在类库工程中添加了app.config文件是吧?我记得配置文件只能添加在可执行的程序中,才能读取吧?你把app.config添加在你的调用这个类库工程的可执行文件中,然后在该类库中使用读取配置文件的函数就ok了 --------------------编程问答-------------------- 关键是app.config添加的工程目录,不应该在类库工程中,应该在可执行工程中 --------------------编程问答-------------------- <?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="HairCuting.Properties.Settings.DataBaseConnectionString"
connectionString="Data Source=|DataDirectory|\DataBase.sdf"
providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
--------------------编程问答-------------------- 一般 connectionString
补充:.NET技术 , C#