在web应用程序中在网页中对Web.config文件进行数据连接配置

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Configuration;
 4 using System.Web.Configuration;
 5 using System.Text;
 6 using System.IO;
 7 
 8     /// <summary> 
 9 /// SystemConfig 的摘要说明 
10 /// </summary> 
11 public static class SystemConfig
12 {
13     private static Configuration ConfigFile;
14     private static KeyValueConfigurationCollection SysConfig;
15 
16     static SystemConfig()
17     {
18         ConfigFile = WebConfigurationManager.OpenWebConfiguration("/");
19         SysConfig = ConfigFile.AppSettings.Settings;
20     }
21 
22     public static void setValue(string configfilepath, string key, string value) 
23     {
24         ExeConfigurationFileMap map = new ExeConfigurationFileMap();
25         map.ExeConfigFilename = configfilepath;
26         ConfigFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
27         SysConfig = ConfigFile.AppSettings.Settings;
28 
29         KeyValueConfigurationElement el = SysConfig[key];
30         if (el == null)
31         {
32             SysConfig.Add(key, value);
33         }
34         else
35         {
36             el.Value = value;
37         }
38 
39         //同步更新
40         ConfigFile.Save();
41 
42     }
43 
44 }

 

posted @ 2013-07-11 10:57  Follow-your-heart  阅读(309)  评论(0编辑  收藏  举报