Exemplo n.º 1
0
        /// <summary>
        /// 将数据源设置实例解析为字符串
        /// </summary>
        /// <param name="shellSettings">数据源设置实例</param>
        /// <returns>数据源设置字符串</returns>
        protected virtual string ComposeSettings(DataSettings shellSettings)
        {
            if (shellSettings == null)
            {
                return(string.Empty);
            }

            var xmlDocument = new XDocument(
                new XElement(
                    "configuration",
                    new XElement("item", new XAttribute("key", "DataProvider"), new XAttribute("value", shellSettings.DataProvider)),
                    new XElement("item", new XAttribute("key", "DataConnectionString"), new XAttribute("value", shellSettings.DataConnectionString))
                    )
                );

            shellSettings.RawDataSettings?.Keys.ToList().ForEach(p =>
            {
                var xElement = new XElement("item", new XAttribute("key", p), new XAttribute("key", shellSettings.RawDataSettings[p]));
                xmlDocument.Root?.Add(xElement);
            });

            #region Old Method

            /* var settingString = new StringBuilder();
             * foreach (var propertiy in typeof(DataSettings).GetProperties())
             * {
             *   if (propertiy.PropertyType.IsCSharpBasicTypeOrOtherBasicType())
             *   {
             *       settingString.AppendFormat("{0}:{1}{2}"
             *           , propertiy.Name
             *           , propertiy.GetValue(shellSettings),
             *           Environment.NewLine);
             *   }
             * }*/
            #endregion

            return(xmlDocument.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存数据源设置实例信息到文件
        /// </summary>
        /// <param name="settings">数据源设置实例</param>
        /// <param name="filePath">文件存储目录</param>
        public virtual void SaveSettings(DataSettings settings, string filePath = null)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (string.IsNullOrEmpty(filePath))
            {
                filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), filename);
            }

            //not exists,create
            if (!File.Exists(filePath))
            {
                using (File.Create(filePath))
                {
                }
            }

            //write
            File.WriteAllText(filePath, this.ComposeSettings(settings));
        }