Wpf 如何从codebehind读取app.config中的自定义部分?
我有一个Wpf 如何从codebehind读取app.config中的自定义部分?,wpf,vb.net,app-config,Wpf,Vb.net,App Config,我有一个app.config文件,它将值存储在几个不同的部分中。我有以下片段: <configuration> <configSections> <sectionGroup name="someDataAccessLayer"> <section name="databaseConnectionStrings" type="sometype" /> </sectionGroup> </con
app.config
文件,它将值存储在几个不同的部分中。我有以下片段:
<configuration>
<configSections>
<sectionGroup name="someDataAccessLayer">
<section name="databaseConnectionStrings" type="sometype" />
</sectionGroup>
</configSections>
<someDataAccessLayer>
<databaseConnectionStrings>
<databaseConnectionString name="someSQL"
value="database=somedatabase;Integrated Security=False;User Id=sa;server=someserver;Password=somepassword/>
</databaseConnectionStrings>
</someDataAccessLayer>
谢谢你的帮助!如果问题仍然不清楚,请告诉我。您的配置部分将与某个.NET类关联以处理它:
<configSections>
<sectionGroup name="someDataAccessLayer">
<section name="databaseConnectionStrings" type="sometype" />
</sectionGroup>
</configSections>
现在您有了一个类型为
sometype
的对象,它包含该配置部分中的所有设置。其中一个属性是数据库连接字符串的列表,您现在可以枚举并找到相应的字符串,然后读取它的.Value
属性。您的配置部分将与某个.NET类关联以处理它:
<configSections>
<sectionGroup name="someDataAccessLayer">
<section name="databaseConnectionStrings" type="sometype" />
</sectionGroup>
</configSections>
现在您有了一个类型为sometype
的对象,它包含该配置部分中的所有设置。其中一个属性是数据库连接字符串列表,您现在可以枚举并找到相应的字符串,然后读取其.Value
属性。App.Config设置:
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="MyApp.LocalConnectionString"
connectionString="Data Source= .\SQLEXPRESS;Initial Catalog=DBName;Integrated Security = true"
providerName="System.Data.SqlClient" />
App.Config设置:
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="MyApp.LocalConnectionString"
connectionString="Data Source= .\SQLEXPRESS;Initial Catalog=DBName;Integrated Security = true"
providerName="System.Data.SqlClient" />
// add reference
using System.Configuration;
// then access connection string in your class
private static string strConnectionString = ConfigurationManager.ConnectionStrings["MyApp.LocalConnectionString"].ConnectionString;