Jack.Peng
2021-04-20 1c60d225cc7c1ccf17c3d1e6a84ca0d959550cf3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
 
namespace Bro.Common.Util
{
 
    /// <summary>
    /// 配置文件帮助类
    /// </summary>
    public class ConfigHelper
    {
 
        private static object ioLock = new object();
 
        /// <summary>
        /// 载入XML配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T Load<T>(string filePath)
        {
            try
            {
 
                lock (ioLock)
                {
                    if (!File.Exists(filePath))
                    {
                        return default(T);
                    }
 
                    var fs = new FileStream(filePath, FileMode.Open);
                    var xs = new XmlSerializer(typeof(T));
 
                    var config = (T)xs.Deserialize(fs);
 
                    fs.Close();
 
                    return config;
                }
            }
            catch(Exception ex)
            {
                Trace.TraceError("ConfigHelper load fail, filepath:{0},ex:{1}", filePath, ex);
            }
 
            return default(T);
        }
 
        /// <summary>
        /// 保存XML配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static bool Save<T>(string filePath, T config)
        {
            try
            {
                lock(ioLock)
                {
 
                    var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
                    var xs = new XmlSerializer(typeof(T));
 
                    xs.Serialize(fs, config);
 
                    fs.Close();
                }
 
                return true;
            }
            catch(Exception ex)
            {
                Trace.TraceError("ConfigHelper Save fail, filepath:{0},ex:{1}", filePath, ex);
            }
        
            return false;
        }
 
        /// <summary>
        /// 修改AppSettings中配置
        /// </summary>
        /// <param name="key">key值</param>
        /// <param name="value">相应值</param>
        public static bool SetConfigValue(string key, string value)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings[key] != null)
                    config.AppSettings.Settings[key].Value = value;
                else
                    config.AppSettings.Settings.Add(key, value);
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}