dev*_*747 0 c# config windows-console
当我刚拿到app.config时,每当我尝试ConfigurationManager.AppSettings [0]或ConfigurationManager.AppSettings ["keyName"]时,我得到一个null.所以我尝试使用*.settings文件创建了一个看起来像这样的app.config
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="IndexLoader.IndexLoader" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<IndexLoader.IndexLoader>
<setting name="ConnectionString" serializeAs="String">
<value>INITIAL CATALOG=xxx;DATA SOURCE=xxx;User ID=xxx;Password=xxx;Application Name=xxx;</value>
</setting>
</IndexLoader.IndexLoader>
</applicationSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
现在我如何使用C#读取此连接字符串?
该解决方案有多个项目,其中许多都有配置文件.有没有办法指定我想要使用代码项目中的配置文件?
applicationSettings不是appSettings.要读取您使用的applicationSettings中的条目
string myCnStr = Properties.Settings.Default.ConnectionString;
Run Code Online (Sandbox Code Playgroud)
在你的情况下它将是
string myCnStr =IndexLoader.IndexLoader.Default.ConnectionString;
Run Code Online (Sandbox Code Playgroud)
但是,ConnectionString在app.config中有一个专用部分,应存储在那里并从ConfigurationManager.ConnectionStrings集合中读取
请参阅此处以获取参考
| 归档时间: |
|
| 查看次数: |
20750 次 |
| 最近记录: |