AppSettingManager

 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     }

 

转载于:https://www.cnblogs.com/gaobing/p/3872830.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值