Thứ Tư, 8 tháng 8, 2012

Cách sử dụng ( đọc, thêm, sửa, xóa ) appSettings trong file web.config C# ASP.NET

Các thao tác đọc, thêm, sửa, xóa appSettings tương tự như các thao tác với app.config. Tuy nhiên có sự khác nhau về cách đọc file config.

Xem thao tác với app.config C#

Ví dụ hàm sửa appSettings:
  1. public static void EditAppSetting(string key, string value)  
  2. {  
  3.     System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");  
  4.     config.AppSettings.Settings[key].Value = value;  
  5.     config.Save(ConfigurationSaveMode.Modified);  
  6.     ConfigurationManager.RefreshSection("appSettings");  
  7. }  
Tham số truyền vào hàm OpenWebConfiguration là đường dẫn thư mục file web.config cần load. Nếu bằng null thì mặc định là file web.config ở thư mục cao nhất.

Note: Nếu gặp lỗi Failed to map the path thì xem Khắc phục lỗi 'Failed to map the path' khi load file web.config

Không có nhận xét nào :

Đăng nhận xét