C#获取web.config配置文件内容

时间:2022-06-29 11:28:51

1.ConfigurationManager提供对客户端应用程序配置文件的访问。

其有两个属性1.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSection 数据。

方法一:

string myConn =System.Configuration.ConfigurationManager.ConnectionStrings["sqlConnectionString"].ConnectionString;

方法二:

string connString =System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionString"].ToString();


web.config中的配置为:
<configuration>
<connectionStrings>
    <add name="sqlConnectionString" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
      providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

2.AppSettings 获取当前应用程序默认配置的 AppSettingsSection 数据。

string myConn = ConfigurationManager.AppSettings["sqlConnectionString"].ToString();

string myConn =System.Configuration.ConfigurationSettings.AppSettings["sqlConnectionString"]

web.config中的配置为:
<configuration>
<appSettings>
    <add key="sqlConnectionString" value="Monday, January 23, 2006 2:56:14 PM" />
</appSettings>
</configuration>