songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
111
112
113
114
115
116
117
118
119
120
121
122
123
/*----------------------------------------------------------------
// Copyright (C) 2009 ¹ãÖݺ²Óî¼ÆËã»ú¿Æ¼¼ÓÐÏÞ¹«Ë¾
// ÎļþÃû£ºConfigHelper.cs
// ×÷ÕߣºÂ½ÈÙ´º
// ÈÕÆÚ£º2009-5-10
// Îļþ¹¦ÄÜÃèÊö£ºÅäÖÃÎļþ¶ÁÈ¡Óë±£´æµÄÀà¡£
// 
// ´´½¨±êʶ£º2009.06.17 °æ±¾£º1.0 LuRongChun ´´½¨¡£
// Ð޸ıêʶ£º
// Ð޸ıêʶ£º
//----------------------------------------------------------------*/
 
using System;
using System.Xml;
using System.Text;
using System.Configuration;
 
namespace BatchService.Framework.Utility
{
    /// <summary>
    /// ÅäÖÃÎļþ¶ÁÈ¡Óë±£´æµÄÀà
    /// </summary> 
    public class ConfigHelper
    {
        #region ¶ÁÈ¡ÅäÖÃÎļþ
 
        /// <summary>
        /// ´ÓApp.ConfigÖеÄappSettingsÖÐÈ¡add...µÄ²ÎÊýÖµ
        /// </summary>
        /// <param name="key">add key='myKey' ÖеÄkeyÖµ(myKey)</param>
        /// <param name="defaultValue">Ô¤ÉèÖµ£¬µ±Ã»ÓÐkey»òkeyûÓÐֵʱ£¬·µ»ØdefaultValue</param>
        /// <returns>keyµÄ¶ÔÓ¦Öµ(add key='myKey' value='myVal'ÖеÄmyVal)</returns>
        public static string ReadConfig(String key, String defaultValue = "")
        {
            try
            {
                Object setting = ConfigurationManager.AppSettings[key];
 
                return (setting == null) ? defaultValue : (string)setting;
            }
            catch
            {
                return defaultValue;
            }
        }
 
        public static string ReadConnectionConfig(String key, String defaultValue = "")
        {
            try
            {
                var setting = ConfigurationManager.ConnectionStrings[key];
 
                return (setting == null) ? defaultValue : setting.ConnectionString;
            }
            catch
            {
                return defaultValue;
            }
        }
 
        #endregion
 
        #region ±£´æÅäÖÃÎļþ
 
        /// <summary>
        /// ±£´æÅäÖÃÎļþ
        /// </summary>
        /// <param name="AppKey"></param>
        /// <param name="AppValue"></param>
        public static void SetKeyValue(string AppKey, string AppValue)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
 
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;
 
            xNode = xDoc.SelectSingleNode("//configuration/appSettings");
 
            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
        }
 
        static public void UpdateConfig(string section, string key, string value)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 
            ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["configuration"].Sections[section];
            SettingElement element = applicationSettingsSection.Settings.Get(key);
 
            if (null != element)
            {
                applicationSettingsSection.Settings.Remove(element);
                element.Value.ValueXml.InnerXml = value;
                applicationSettingsSection.Settings.Add(element);
            }
            else
            {
                element = new SettingElement(key, SettingsSerializeAs.String);
                element.Value = new SettingValueElement();
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                element.Value.ValueXml = doc.CreateElement("value");
 
                element.Value.ValueXml.InnerXml = value;
                applicationSettingsSection.Settings.Add(element);
            }
 
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("configuration");
        }
 
        #endregion
    }
}