.Net 配置的簡陋解決方案

    公司是作CS產品的, 最近分配給我一個活, 要求:
    1. 公司程序啓動時, 檢測是否有配置文件, 沒有的話則按默認值建立一個
    2. 配置文件要加密, 不能讓客戶隨便看到裏面的參數
    3. 配置文件要有配套的GUI配置工具, 由於現場實施人員嫌XML配置麻煩

    若是隻有一個產品須要這個功能, 把每一個配置項的讀寫功能硬編碼寫到工具裏就完事了, 但公司有好幾個產品都須要這個, 不得不寫一個通用的工具類

    這個工做還解決了兩個問題:
    a. 之前設置項都配置在 app.config 裏, 每次升級都會覆蓋原來的設置, 因此現場人員都必須先將 app.config複製出來.
    b. app.config 裏新增了配置項, 現場實施人員必須仔細對比, 將新增項人工放入原來的app.config

    如今的作法是, 配置文件ConfigSetting.xml並不在安裝包中, 因此卸載升級都不會影響它; 程序第一次啓動時, 會按默認值生成一個ConfigSetting.xml; 之後程序啓動的時候, 假若有新增的配置項, 則將其加入ConfigSetting.xml

    我把涉及的兩個類都放在了一個文件, 這樣引入一個文件便可數據庫

    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using System.Security.Cryptography;
    using System.IO;
    /// <summary>
    /// 設置項的幫助類
    /// </summary>
    public class ConfigSettingTool
    {
        /// <summary>
        /// 保存讀取時, 是否加密解密; 設爲false, 能夠方便調試
        /// 其實也只能防小白, 隨便反編譯一下就啥都漏出來了
        /// </summary>
        private static bool isEncrypt = false;

        /// <summary>
        /// 默認的配置文件名
        /// </summary>
        public static readonly string DefaultXmlFileName = "ConfigSetting.xml";

        /// <summary>
        /// 獲取XDocument, 解密失敗、XML結構不合理, 都會根據模板從新生成一個
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <param name="msg"></param>
        /// <returns>確保返回以下格式
        /// <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        /// <Setting>
        ///   <SingleSetting></SingleSetting>
        /// </Setting>
        /// </returns>
        public static XDocument GetXDocument(string xmlFileName, out string msg)
        {
            msg = null;
            if (!System.IO.File.Exists(xmlFileName))
            {
                msg = "配置文件不存在, 建立默認配置文件";
                return new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("Setting", new XElement("SingleSetting")));
            }
            try
            {
                var textContent = System.IO.File.ReadAllText(xmlFileName);
                textContent = isEncrypt ? Decrypt(textContent) : textContent;
                var xdoc = XDocument.Parse(textContent);
                if (xdoc.Root.Name != "Setting")
                {
                    throw new Exception("根節點不是 Setting");
                }
                if (xdoc.Root.Element("SingleSetting") == null)
                {
                    throw new Exception("沒有 SingleSetting 節點");
                }
                return xdoc;
            }
            catch
            {
                msg = "配置文件不是標準格式, 刪除後, 建立默認配置文件";
                return new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("Setting", new XElement("SingleSetting")));
            }
        }


        /// <summary>
        /// 將xml信息讀出到settingArray, 若是缺乏某項設定則增長到xdoc
        /// </summary>
        /// <param name="xdoc"></param>
        /// <param name="settingList">一般就是GlobalSetting.DefaultGlobalSettingArray</param>
        public static void ReadValueToSettingArray(XDocument xdoc, List<ConfigureItemModel> settingArray)
        {
            var singleSettingElement = xdoc.Root.Element("SingleSetting");
            foreach (var configureItem in settingArray)
            {
                configureItem.ErrorMsg = null;
                var element = singleSettingElement.Element(configureItem.Name);
                if (element == null)
                {
                    element = new XElement(configureItem.Name, configureItem.DefaultValue, new XAttribute("Caption", configureItem.Caption), new XAttribute("Description", configureItem.Description), new XAttribute("DefaultValue", configureItem.DefaultValue), new XAttribute("CanBeEmpty", configureItem.CanBeEmpty));
                    singleSettingElement.Add(element);
                }
                configureItem.Value = string.IsNullOrWhiteSpace(element.Value) ? "" : element.Value.Trim();
            }
        }


        /// <summary>
        /// 將xml信息讀出到settingArray
        /// </summary>
        /// <param name="xdoc"></param>
        /// <param name="settingList">一般就是GlobalSetting.DefaultGlobalSettingArray</param>
        public static void ReadConfig(XDocument xdoc, out List<ConfigureItemModel> settingList)
        {
            settingList = new List<ConfigureItemModel>();

            var singleSettingElement = xdoc.Root.Element("SingleSetting");
            foreach (var element in singleSettingElement.Elements())
            {
                var captionAttribute = element.Attribute("Caption");
                var caption = captionAttribute != null ? captionAttribute.Value : "";

                var name = element.Name.ToString();
                var value = element.Value.ToString();

                var descriptionAttribute = element.Attribute("Description");
                var description = descriptionAttribute != null ? descriptionAttribute.Value : "";

                var defaultValueAttribute = element.Attribute("DefaultValue");
                var defaultValue = defaultValueAttribute != null ? defaultValueAttribute.Value : "";

                var canBeEmpty = false;
                try
                {
                    canBeEmpty = bool.Parse(element.Attribute("CanBeEmpty").Value);
                }
                catch { }

                var errorMsgAttribute = element.Attribute("ErrorMsg");
                var errorMsg = errorMsgAttribute != null ? errorMsgAttribute.Value : "";


                var configureItem = new ConfigureItemModel(caption, name, defaultValue, description, canBeEmpty) { Value = value, ErrorMsg = errorMsg };
                settingList.Add(configureItem);
            }
        }


        /// <summary>
        /// 嘗試解析設置內容 到 目標class
        /// </summary>
        /// <param name="settingList">一般就是GlobalSetting.DefaultGlobalSettingArray, 配置項設置不合理時, 會將錯誤信息保存到ErrorMsg</param>
        /// <param name="targetSettingClass">一般就是GlobalSetting</param>
        /// <returns>成功, true; 失敗: false</returns>
        public static bool TryParseConfig(List<ConfigureItemModel> settingArray, Type targetSettingClass)
        {
            bool isAllSuccess = true;
            foreach (var configureItem in settingArray)
            {
                configureItem.ErrorMsg = null;
                configureItem.Value = string.IsNullOrWhiteSpace(configureItem.Value) ? "" : configureItem.Value.Trim();
                if (configureItem.Value == "" && configureItem.CanBeEmpty == false)
                {
                    configureItem.ErrorMsg += "該項值不能爲空, 請手動填寫該值;";
                    isAllSuccess = false;
                    continue;
                }

                var property = targetSettingClass.GetProperty(configureItem.Name);
                //若是 targetSettingClass 沒有對應的靜態屬性, 則跳過
                if (property == null)
                {
                    continue;
                }
                object value = null;
                try
                {
                    value = Convert.ChangeType(configureItem.Value, property.PropertyType);
                    property.SetValue(null, value, null);
                }
                catch
                {
                    configureItem.ErrorMsg += configureItem.Value + "不能轉換爲" + property.PropertyType.Name + ", 請從新填寫該值;";
                    isAllSuccess = false;
                    continue;
                }
            }
            return isAllSuccess;
        }


        /// <summary>
        /// 寫入
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <param name="settingList">一般就是GlobalSetting.DefaultGlobalSettingArray</param>
        /// <returns>成功, null</returns>
        public static bool TrySaveToXML(string xmlFileName, List<ConfigureItemModel> settingArray, out string msg)
        {
            msg = null;
            var xdoc = GetXDocument(xmlFileName, out  msg);//原文件讀出錯誤, 忽略便可, 由於settingArray會自動填充
            var singleSettingElement = xdoc.Root.Element("SingleSetting");

            foreach (var configureItem in settingArray)
            {
                var element = singleSettingElement.Element(configureItem.Name);
                if (element == null)
                {
                    element = new XElement(configureItem.Name, configureItem.Value);
                    singleSettingElement.Add(element);
                }
                else
                {
                    element.Value = configureItem.Value ?? "";
                }
                element.RemoveAttributes();
                element.Add(new XAttribute("Caption", configureItem.Caption));
                element.Add(new XAttribute("Description", configureItem.Description));
                element.Add(new XAttribute("DefaultValue", configureItem.DefaultValue));
                element.Add(new XAttribute("CanBeEmpty", configureItem.CanBeEmpty));
                if (!string.IsNullOrWhiteSpace(configureItem.ErrorMsg))
                {
                    element.Add(new XAttribute("ErrorMsg", configureItem.ErrorMsg));
                }
            }


            var textContent = xdoc.ToString();
            textContent = isEncrypt ? Encrypt(textContent) : textContent;
            try
            {
                System.IO.File.WriteAllText(xmlFileName, textContent);
                return true;
            }
            catch (Exception ex)
            {
                msg= "保存失敗:" + ex.Message;
                return false;
            }
        }

        #region 加密解密部分
        private static byte[] DESKey = new byte[] { 11, 69, 93, 102, 172, 41, 18, 12 };
        private static byte[] DESIV = new byte[] { 75, 77, 46, 197, 78, 157, 23, 36 };

        /// <summary>
        /// 加密
        /// </summary>
        private static string Encrypt(string source)
        {
            string reValue = "";
            DESCryptoServiceProvider objDes = new DESCryptoServiceProvider();
            MemoryStream objMemoryStream = new MemoryStream();
            CryptoStream objCrytoStream = new CryptoStream(objMemoryStream, objDes.CreateEncryptor(DESKey, DESIV), CryptoStreamMode.Write);
            StreamWriter objStreamWriter = new StreamWriter(objCrytoStream);
            objStreamWriter.Write(source);
            objStreamWriter.Flush();
            objCrytoStream.FlushFinalBlock();
            objMemoryStream.Flush();
            reValue = Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)objMemoryStream.Length);
            return reValue;
        }

        /// <summary>
        /// 解密
        /// </summary>
        private static string Decrypt(string source)
        {
            string reValue = "";
            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
            byte[] Input = Convert.FromBase64String(source);
            MemoryStream objMemoryStream = new MemoryStream(Input);
            CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateDecryptor(DESKey, DESIV), CryptoStreamMode.Read);
            StreamReader objStreamReader = new StreamReader(objCryptoStream);
            reValue = objStreamReader.ReadToEnd();
            return reValue;
        }
        #endregion
    }

    /// <summary>
    /// 單個設置項
    /// </summary>
    /// <remarks>因爲XML中不能保存null, 因此全部屬性都不會被設置爲null</remarks>
    public class ConfigureItemModel
    {
        /// <summary>
        /// 單個設置項
        /// </summary>
        /// <param name="captionParam">顯示名稱</param>
        /// <param name="nameParam">參數名稱</param>
        /// <param name="defaultValueParam">默認值</param>
        /// <param name="descriptionParam">描述, 該項不設定時候, 顯示默認值</param>
        /// <param name="canBeEmptyParam">可否爲空字符串</param>
        public ConfigureItemModel(string captionParam, string nameParam, string defaultValueParam, string descriptionParam = "", bool canBeEmptyParam = false)
        {
            Caption = captionParam;
            Name = nameParam;
            Description = descriptionParam;
            DefaultValue = defaultValueParam;
            CanBeEmpty = canBeEmptyParam;
        }



        private string caption = "";
        /// <summary>
        /// 顯示名稱
        /// </summary>
        public string Caption
        {
            get { return caption; }
            set { caption = string.IsNullOrWhiteSpace(value) ? "" : value; }
        }



        private string name = "";
        /// <summary>
        /// 參數名稱
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = string.IsNullOrWhiteSpace(value) ? "" : value; ; }
        }


        private string description = "";
        /// <summary>
        /// 說明, 若是該值沒有賦值, 則顯示DefaultValue
        /// </summary>
        public string Description
        {
            get { return string.IsNullOrWhiteSpace(description) ? defaultValue : description; }
            set { description = string.IsNullOrWhiteSpace(value) ? "" : value; }
        }


        private string defaultValue = "";
        /// <summary>
        /// 默認值
        /// </summary>
        public string DefaultValue
        {
            get { return defaultValue; }
            set { defaultValue = string.IsNullOrWhiteSpace(value) ? "" : value; }
        }


        /// <summary>
        /// 可否爲空字符串
        /// </summary>
        public bool CanBeEmpty { get; set; }

        /// <summary>
        /// 可否爲空字符串 的字符串形式
        /// </summary>
        public string CanBeEmptyString { get { return CanBeEmpty ? "" : ""; } }


        private string innerValue = "";
        /// <summary>
        ////// </summary>
        public string Value
        {
            get { return innerValue; }
            set { innerValue = string.IsNullOrWhiteSpace(value) ? "" : value; ; }
        }


        private string errorMsg = "";
        /// <summary>
        /// 錯誤信息
        /// </summary>
        public string ErrorMsg
        {
            get { return errorMsg; }
            set { errorMsg = string.IsNullOrWhiteSpace(value) ? "" : value; ; }
        }

    }
View Code

 

   產品裏建一個 GlobalSetting 類, 裏面的配置項都必須是  static 屬性, 而後加入 public static List<ConfigureItemModel> DefaultGlobalSettingArray 保存默認設置app

    /// <summary>
    /// 全局設定
    /// </summary>
    /// <remarks>全部屬性都必須是 static , 即 類屬性</remarks>
    public class GlobalSetting
    {
        public static List<ConfigureItemModel> DefaultGlobalSettingArray = new List<ConfigureItemModel>()
        {
            new ConfigureItemModel("數據庫的主機地址","ConnectionStringHost", "127.0.0.1"),
        };

        /// <summary>
        /// 數據庫的主機地址
        /// </summary>
        public static string ConnectionStringHost { get; set; }
    }
View Code

 

  具體的示例請參考 ConfigSettingToolTest, 第一次運行時會報錯: 升級地址 不能爲空, 使用  配置文件編輯工具2.exe 爲其賦值後, 就能夠正常啓動了ide

 

ConfigSettingToolTest              ConfigEditer工具

相關文章
相關標籤/搜索