/// <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(nameof(settings)); } if (string.IsNullOrEmpty(filePath)) { filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), Filename); } //如果不存在则创建 if (!File.Exists(filePath)) { using (File.Create(filePath)) { } } File.WriteAllText(filePath, this.ComposeSettings(settings)); }
/// <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 ?? string.Empty)), new XElement("item", new XAttribute("key", "DataConnectionString"), new XAttribute("value", shellSettings.DataConnectionString ?? string.Empty)) ) ); 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()); }