首页 > 移动平台 > 详细

AppSettingManager

时间:2014-07-28 15:19:13      阅读:249      评论:0      收藏:0      [点我收藏+]
 1     public class AppSettingManager
 2     {
 3         public static bool Update(string key, string value)
 4         {
 5 
 6             try
 7             {
 8                 var config = Create();
 9                 if (config == null)
10                 {
11                     return false;
12                 }
13                 var isModified = !string.IsNullOrEmpty(Get(key));
14                 if (isModified)
15                 {
16                     config.AppSettings.Settings.Remove(key);
17                 }
18                 // Add an Application Setting.
19                 config.AppSettings.Settings.Add(key, value);
20                 // Save the changes in App.config file.
21                 config.Save(ConfigurationSaveMode.Modified);
22                 // Force a reload of a changed section.
23                 ConfigurationManager.RefreshSection("appSettings");
24                 return true;
25             }
26             catch (Exception)
27             {
28                 return false;
29             }
30 
31         }
32 
33         public static bool Add(string key, string value)
34         {
35             try
36             {
37                 var config = Create();
38                 if (config == null)
39                 {
40                     return false;
41                 }
42                 config.AppSettings.Settings.Add(key, value);
43                 config.Save(ConfigurationSaveMode.Modified);
44                 ConfigurationManager.RefreshSection("appSettings");
45                 return true;
46             }
47             catch (Exception)
48             {
49                 return false;
50 
51             }
52 
53         }
54 
55         public static string Get(string key)
56         {
57             var config = Create();
58             if (config == null)
59             {
60                 return null;
61             }
62             return config.AppSettings.Settings[key].Value;
63         }
64 
65         private static Configuration Create()
66         {
67             try
68             {
69                 return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
70             }
71             catch (Exception)
72             {
73                 return null;
74             }
75         }
76     }

 

AppSettingManager,布布扣,bubuko.com

AppSettingManager

原文:http://www.cnblogs.com/gaobing/p/3872830.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!