Beispiel #1
0
        public void Save(string fileName, bool encrypted)
        {
            string fullName = new System.IO.FileInfo(fileName).FullName;
            ConfigurationDocument document = new ConfigurationDocument();
            ConfigurationGroup    group    = document.Add(GroupKeys.DataBase);

            group.Add(ItemKeys.DataSource, DataSource, encrypted);
            group.Add(ItemKeys.InitialCatalog, InitialCatalog, encrypted);
            group.Add(ItemKeys.UserID, UserID, encrypted);
            group.Add(ItemKeys.Password, Password, encrypted);
            document.Save(fullName);
        }
        /// <summary>
        /// 加载配置文件。
        /// </summary>
        /// <param name="fileName">文件名。</param>
        /// <returns>包含所指定文件内容的配置文件。</returns>
        public static ConfigurationDocument LoadFrom(string fileName)
        {
            // 创建文件对象。
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
            // 如果文件不存在,则返回空的配置文件对象。
            if (!fileInfo.Exists)
            {
                return(new ConfigurationDocument());
            }
            // 从文件中加载Xml文档对象。
            System.Xml.Linq.XDocument xDocument = System.Xml.Linq.XDocument.Load(fileInfo.FullName);
            // 如果Xml文档的根节点名称不是默认的根节点名称,则返回空的配置文件对象。
            if (xDocument.Root.Name.LocalName != XmlRootName)
            {
                return(new ConfigurationDocument());
            }
            // 创建配置文件对象。
            ConfigurationDocument document = new ConfigurationDocument();

            // 遍历Xml文档根节点下的Xml组节点。
            foreach (System.Xml.Linq.XElement xGroup in xDocument.Root.Elements())
            {
                // 如果Xml组节点的名称是默认的组节点的名称,则继续。
                if (xGroup.Name.LocalName == XmlGroupNodeName)
                {
                    // 获取Xml组节点的key属性。
                    System.Xml.Linq.XAttribute xGroupKeyAttribute = xGroup.Attribute(XmlKeyAttributeName);
                    // 如果key属性不为空且属性值不为空,则继续。
                    if (xGroupKeyAttribute != null)
                    {
                        // 根据xml组节点的key属性值创建并添加配置组对象。
                        ConfigurationGroup configurationGroup = document.GetOrAdd(xGroupKeyAttribute.Value);
                        // 遍历Xml组节点下的Xml项节点。
                        foreach (System.Xml.Linq.XElement xItem in xGroup.Elements())
                        {
                            // 如果Xml项节点的名称是默认的项节点名称,则继续。
                            if (xItem.Name.LocalName == XmlItemNodeName)
                            {
                                // 获取Xml项节点的key属性、value属性、encrypted属性。
                                System.Xml.Linq.XAttribute xItemKeyAttribute       = xItem.Attribute(XmlKeyAttributeName);
                                System.Xml.Linq.XAttribute xItemValueAttribute     = xItem.Attribute(XmlValueAttributeName);
                                System.Xml.Linq.XAttribute xItemEncryptedAttribute = xItem.Attribute(XmlEncryptedAttributeName);
                                // 如果key属性不为空,且key属性值不为空
                                if (xItemKeyAttribute != null)
                                {
                                    // 创建配置项。
                                    ConfigurationItem configurationItem = configurationGroup.GetOrAdd(xItemKeyAttribute.Value);
                                    // 如果value属性不为空,则赋值给配置项。
                                    if (xItemValueAttribute != null)
                                    {
                                        configurationItem.Value = xItemValueAttribute.Value;
                                    }
                                    // 如果encrypted属性不为空,且encrypted属性值不为空,则解密配置项的值。
                                    if (xItemEncryptedAttribute != null && !string.IsNullOrWhiteSpace(xItemEncryptedAttribute.Value))
                                    {
                                        // 如果encrypted属性可以转换成bool类型的值。
                                        if (bool.TryParse(xItemEncryptedAttribute.Value, out bool encrypted))
                                        {
                                            // 赋值配置项的加密属性。
                                            configurationItem.Encrypted = encrypted;
                                            // 根据配置项的加密属性值来解密配置项的值。
                                            if (encrypted)
                                            {
                                                if (Studio.Security.DESManager.TryDecrypt(configurationItem.Value, out string value))
                                                {
                                                    configurationItem.Value = value;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(document);
        }