C#使用ConfigurationManager類操作設定檔

來源:互聯網
上載者:User
C#使用ConfigurationManager類操作設定檔  

.net1.1中如果需要靈活的操作和讀寫設定檔並不是十分方便,一般都會在項目中封裝一個配置 檔案管理類來進行讀寫操作。而在.net2.0中使用configurationmanager 和webconfigurationmanager 類可以很好的管理設定檔,configurationmanager類在system.configuration中, webconfigurationmanager在system.web.configuration中。根據msdn的解釋,對於 web 應用程式配置,建議使用
System.Web.Configuration.WebConfigurationManager 類,而不要使用 system.configuration.configurationmanager 類。

下面我給出一個簡單的例子說明如何使用webconfigurationmanager操作設定檔:
//開啟設定檔
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//擷取appsettings節點
AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
//在appsettings節點中添加元素
appsection.Settings.Add("addkey1", "key1s value");
appsection.Settings.Add("addkey2", "key2s value");
config.Save();

運行代碼之後可以看見設定檔中的改變:

<appsettings>
<add key="addkey1" value="key1s value" />
<add key="addkey2" value="key2s value" />
</appsettings>
修改和刪除節點或屬性也非常方便:

//開啟設定檔
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//擷取appsettings節點
AppSettingsSection appsection = ( AppSettingsSection)config.GetSection("appSettings");
//刪除appsettings節點中的元素
appsection.Settings.Remove("addkey1");
//修改appsettings節點中的元素
appsection.Settings["addkey2"].Value = "modify key2s value";
config.Save();
設定檔:
<appsettings>
<add key="addkey2" value="modify key2s value" />
</appsettings>
===============================================================================

往web.config中寫資料庫連接字串

protected void Page_Load(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();

builder.DataSource = "localhost";
builder.InitialCatalog = "Northwind1";
builder.UserID = "sa";
builder.Password = "password";
builder.PersistSecurityInfo = true;

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//config.ConnectionStrings.ConnectionStrings["AppConnectionString"].ConnectionString = builder.ConnectionString;
AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
appsection.Settings.Add("connstr", builder.ConnectionString);
config.Save();
}

.net1.1中如果需要靈活的操作和讀寫設定檔並不是十分方便,一般都會在項目中封裝一個配置 檔案管理類來進行讀寫操作。而在.net2.0中使用configurationmanager 和webconfigurationmanager 類可以很好的管理設定檔,configurationmanager類在system.configuration中, webconfigurationmanager在system.web.configuration中。根據msdn的解釋,對於 web 應用程式配置,建議使用
System.Web.Configuration.WebConfigurationManager 類,而不要使用 system.configuration.configurationmanager 類。

下面我給出一個簡單的例子說明如何使用webconfigurationmanager操作設定檔:
//開啟設定檔
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//擷取appsettings節點
AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
//在appsettings節點中添加元素
appsection.Settings.Add("addkey1", "key1s value");
appsection.Settings.Add("addkey2", "key2s value");
config.Save();

運行代碼之後可以看見設定檔中的改變:

<appsettings>
<add key="addkey1" value="key1s value" />
<add key="addkey2" value="key2s value" />
</appsettings>
修改和刪除節點或屬性也非常方便:

//開啟設定檔
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
//擷取appsettings節點
AppSettingsSection appsection = ( AppSettingsSection)config.GetSection("appSettings");
//刪除appsettings節點中的元素
appsection.Settings.Remove("addkey1");
//修改appsettings節點中的元素
appsection.Settings["addkey2"].Value = "modify key2s value";
config.Save();
設定檔:
<appsettings>
<add key="addkey2" value="modify key2s value" />
</appsettings>
===============================================================================

往web.config中寫資料庫連接字串

protected void Page_Load(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();

builder.DataSource = "localhost";
builder.InitialCatalog = "Northwind1";
builder.UserID = "sa";
builder.Password = "password";
builder.PersistSecurityInfo = true;

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
//config.ConnectionStrings.ConnectionStrings["AppConnectionString"].ConnectionString = builder.ConnectionString;
AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
appsection.Settings.Add("connstr", builder.ConnectionString);
config.Save();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.