ConfigAppSettings.cs 7.3 KB
using System;
using System.Configuration;
using System.Threading;
using System.Xml;
using System.Windows.Forms;

namespace OnlineStore.Common
{
    public class ConfigAppSettings
    {
        //public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private static int seq = 1;

        public static int nextSeq()
        {
            if (seq.Equals(Int32.MaxValue))
            {
                LogUtil.info("seq当前值:" + seq + ",重置seq=0");
                seq = 0;
            }
            Interlocked.Increment(ref seq);
            return seq;
        }
        public static string GetValue(string keyStr, string storeStr)
        {
            string key = keyStr + storeStr;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                return GetValue(keyStr);
            }
            else
            {
                return config.AppSettings.Settings[key].Value;
            }
        }

        public static decimal GetNumValue(string keyStr, string storeStr)
        {
            string key = keyStr + storeStr;
            decimal a = 0;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                return GetNumValue(keyStr);
            }
            else
            {
                {
                    Decimal.TryParse(config.AppSettings.Settings[key].Value, out a);
                }
            }
            return a;
        }
        public static int GetIntValue(string keyStr, string storeStr)
        {
            string key = keyStr + storeStr;
            int a = 0;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                return GetIntValue(keyStr);
            }
            else
            {
                {
                    Int32.TryParse(config.AppSettings.Settings[key].Value, out a);
                }
            } return a;
        }
        public static string GetValue(string key)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                LogUtil.error("未找到配置:" + key + ",请检查配置是否完整!");
                return "";
            }
            else
            {
                return config.AppSettings.Settings[key].Value;
            }
        }

        public static decimal GetNumValue(string key)
        {
            decimal a = 0;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                LogUtil.error("未找到配置:" + key + ",请检查配置是否完整!");
                return a;
            }
            else
            {
                {
                    Decimal.TryParse(config.AppSettings.Settings[key].Value, out a);
                }
            }
            return a;
        }
        public static int GetIntValue(string key)
        {
            int a = 0;
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                LogUtil.error("未找到配置:" + key + ",请检查配置是否完整!");
                return a; 
            }
            else
            {
                {
                    Int32.TryParse(config.AppSettings.Settings[key].Value, out a);
                }
            } return a;
        }
        public static void SaveValue(string key, int value)
        {
            SaveValue(key, value.ToString());
        }
        public static void SaveValue(string key, string value)
        {
            try
            {
                if (key.Equals("") || value.Equals(""))
                {
                    return;
                }
                //增加的内容写在appSettings段下 <add key="RegCode" value="0"/> 
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings[key] == null)
                {
                    SetValue(key, value);
                }
                else
                {
                    UpdateConfig(key, value);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error( "SaveValue保存配置出错:AppKey=" + key + ",AppValue=" + value + ",",ex);
            }
        }

        public static string GetValue(object debugDeviceId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 更新配置文件信息
        /// </summary>
        /// <param name="name">配置文件字段名称</param>
        /// <param name="Xvalue">值</param>
        private static void UpdateConfig(string name, string Xvalue)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Application.ExecutablePath + ".config");
                XmlNode node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
                XmlElement ele = (XmlElement)node;
                ele.SetAttribute("value", Xvalue);
                doc.Save(Application.ExecutablePath + ".config");
            }
            catch (Exception ex)
            {
                LogUtil.error( "UpdateConfig保存配置出错:name=" + name + ",Xvalue=" + Xvalue + ",",ex);
            }
        }
 

        ///<summary>  
        ///向.config文件的appKey结写入信息AppValue   保存设置  
        ///</summary>  
        ///<param name="AppKey">节点名</param>  
        ///<param name="AppValue">值</param>
        private static void SetValue(String AppKey, String AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
                XmlNode xNode;
                XmlElement xElem1;
                XmlElement xElem2;
                xNode = xDoc.SelectSingleNode("//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");
            }
            catch (Exception ex)
            {
                LogUtil.error( "SetValue保存配置出错:AppKey=" + AppKey + ",AppValue=" + AppValue + ",",ex);
            }
        }
    }
}