Code
1 public class ConfigOperation
2 {
3 static Configuration config;
4 static ConfigOperation()
5 {
6
7 }
8 static Configuration GetConfig()
9 {
10 if (config == null)
11 {
12 config = WebConfigurationManager.OpenWebConfiguration("~");
13 }
14 return config;
15 }
16 private static bool IsAppSettingsExistKey(string key)
17 {
18
19 Configuration config = GetConfig();
20 foreach (string str in config.AppSettings.Settings.AllKeys)
21 {
22 if (str == key)
23 {
24 return true;
25 }
26 }
27 return false ;
28 }
29 public static string GetValueByKey(string key)
30 {
31 if (IsAppSettingsExistKey(key))
32 {
33 Configuration config = GetConfig();
34 return config.AppSettings.Settings[key].Value;
35 }
36 return "none";
37 }
38 public static bool SetValuebyKey(string key, string value)
39 {
40 if (IsAppSettingsExistKey(key))
41 {
42 Configuration config = GetConfig();
43 config.AppSettings.Settings[key].Value = value;
44 config.Save(ConfigurationSaveMode.Modified);
45 return true;
46 }
47 else
48 {
49 return false;
50 }
51 }
52 public static bool AddAppSetting(string key, string value)
53 {
54 bool issucceed = false;
55 try
56 {
57 if (IsAppSettingsExistKey(key))
58 {
59 SetValuebyKey(key, value);
60 issucceed = true;
61
62 }
63 else
64 {
65 Configuration config = GetConfig();
66 config.AppSettings.Settings.Add(key, value);
67 config.Save(ConfigurationSaveMode.Modified);
68 issucceed = true;
69 }
70 }
71 catch
72 {
73 issucceed = false;
74 }
75 return issucceed;
76 }
77 }
78
1 public class ConfigOperation
2 {
3 static Configuration config;
4 static ConfigOperation()
5 {
6
7 }
8 static Configuration GetConfig()
9 {
10 if (config == null)
11 {
12 config = WebConfigurationManager.OpenWebConfiguration("~");
13 }
14 return config;
15 }
16 private static bool IsAppSettingsExistKey(string key)
17 {
18
19 Configuration config = GetConfig();
20 foreach (string str in config.AppSettings.Settings.AllKeys)
21 {
22 if (str == key)
23 {
24 return true;
25 }
26 }
27 return false ;
28 }
29 public static string GetValueByKey(string key)
30 {
31 if (IsAppSettingsExistKey(key))
32 {
33 Configuration config = GetConfig();
34 return config.AppSettings.Settings[key].Value;
35 }
36 return "none";
37 }
38 public static bool SetValuebyKey(string key, string value)
39 {
40 if (IsAppSettingsExistKey(key))
41 {
42 Configuration config = GetConfig();
43 config.AppSettings.Settings[key].Value = value;
44 config.Save(ConfigurationSaveMode.Modified);
45 return true;
46 }
47 else
48 {
49 return false;
50 }
51 }
52 public static bool AddAppSetting(string key, string value)
53 {
54 bool issucceed = false;
55 try
56 {
57 if (IsAppSettingsExistKey(key))
58 {
59 SetValuebyKey(key, value);
60 issucceed = true;
61
62 }
63 else
64 {
65 Configuration config = GetConfig();
66 config.AppSettings.Settings.Add(key, value);
67 config.Save(ConfigurationSaveMode.Modified);
68 issucceed = true;
69 }
70 }
71 catch
72 {
73 issucceed = false;
74 }
75 return issucceed;
76 }
77 }
78