2012年11月12日 星期一

ASP.NET C# app.config與web.config的增加、修改、刪除操作

from http://www.dotblogs.com.tw/box5068/archive/2011/02/21/21477.aspx





以一小段App.config的內容來實作:
組態檔的內容如下:
1<!--?xml version="1.0" encoding="utf-8" ?-->
2<configuration>
3  <appsettings>
4    <add key="A" value="This is A value">
5  </add>
6</appsettings>
7</configuration>
1.讀取值:
Asp.Net:System.Web.Configuration.WebConfigurationManager.AppSettings["A"];
WinForm:System.Configuration.ConfigurationManager.AppSettings["A"];  
2.增加
ASP.NET(需要有寫入權限)
1//Configuration與AppSettingsSection必須引用System.Configuration才可使用!
2Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
3AppSettingsSection app = config.AppSettings;
4app.Settings.Add("B""This is B value");
5config.Save(ConfigurationSaveMode.Modified);
WinForm
1//Configuration與AppSettingsSection必須引用System.Configuration才可使用!
2Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
3AppSettingsSection app = config.AppSettings;
4app.Settings.Add("B""This is B value");
5config.Save(ConfigurationSaveMode.Modified);
3.修改
ASP.NET(需要有寫入權限)
1//Configuration與AppSettingsSection必須引用System.Configuration才可使用!
2Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
3AppSettingsSection app = config.AppSettings;
4//app.Settings.Add("B", "This is B value");
5app.Settings["A"].Value = "This is not B";
6config.Save(ConfigurationSaveMode.Modified);
WinForm
1//Configuration與AppSettingsSection必須引用System.Configuration才可使用!
2Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
3AppSettingsSection app = config.AppSettings;
4//app.Settings.Add("B", "This is B value");
5app.Settings["A"].Value = "This is not B";
6config.Save(ConfigurationSaveMode.Modified);
4.刪除
ASP.NET(需要有寫入權限)
1//Configuration與AppSettingsSection必須引用System.Configuration才可使用!
2Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
3AppSettingsSection app = config.AppSettings;
4//app.Settings.Add("B", "This is B value");
5//app.Settings["A"].Value = "This is not B";
6app.Settings.Remove("A");
7config.Save(ConfigurationSaveMode.Modified);
WinForm
1//Configuration與AppSettingsSection必須引用System.Configuration才可使用!
2Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
3AppSettingsSection app = config.AppSettings;
4//app.Settings.Add("B", "This is B value");
5//app.Settings["A"].Value = "This is not B";
6app.Settings.Remove("A");
7config.Save(ConfigurationSaveMode.Modified);
以上就是對組態檔的增加/修改/刪除
另外也可以將連線資料庫的連線字串寫在組態檔內方便修改
如下:
組態檔內容:
1<configuration>
2  <connectionstrings>
3    <add connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\soft.mdb;Persist Security Info=True" name="connDB" providername="System.Data.OleDb">
4  </add></connectionstrings>
5  <appsettings>
6    <add key="connDB2" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\soft.mdb;Persist Security Info=True">
7  </add></appsettings>
8</configuration>
讀取:
1ConfigurationManager.ConnectionStrings["connDB"].ConnectionString;
2//讀取name為connDB的連線字串

0 意見:

張貼留言

 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template