/// <summary> /// Composes the settings. /// </summary> /// <param name="settings">The settings.</param> /// <returns>System.String.</returns> protected virtual string ComposeSettings(DataSettings settings) { if (settings == null) return ""; return string.Format("DataProvider: {0}{2}DataConnectionString: {1}{2}", settings.DataProvider, settings.DataConnectionString, Environment.NewLine ); }
/// <summary> /// Saves the settings. /// </summary> /// <param name="settings">The settings.</param> /// <exception cref="System.ArgumentNullException">settings</exception> public virtual void SaveSettings(DataSettings settings) { if (settings == null) throw new ArgumentNullException("settings"); //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests string filePath = Path.Combine(MapPath("~/App_Data/"), filename); if (!File.Exists(filePath)) { using (File.Create(filePath)) { //we use 'using' to close the file after it's created } } var text = ComposeSettings(settings); File.WriteAllText(filePath, text); }
/// <summary> /// Saves the settings. /// </summary> /// <param name="settings">The settings.</param> /// <exception cref="System.ArgumentNullException">settings</exception> public virtual void SaveSettings(DataSettings settings) { if (settings == null) { throw new ArgumentNullException("settings"); } //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests string filePath = Path.Combine(MapPath("~/App_Data/"), filename); if (!File.Exists(filePath)) { using (File.Create(filePath)) { //we use 'using' to close the file after it's created } } var text = ComposeSettings(settings); File.WriteAllText(filePath, text); }
/// <summary> /// Parses the settings. /// </summary> /// <param name="text">The text.</param> /// <returns>DataSettings.</returns> protected virtual DataSettings ParseSettings(string text) { var shellSettings = new DataSettings(); if (String.IsNullOrEmpty(text)) return shellSettings; //Old way of file reading. This leads to unexpected behavior when a user's FTP program transfers these files as ASCII (\r\n becomes \n). //var settings = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var settings = new List<string>(); using (var reader = new StringReader(text)) { string str; while ((str = reader.ReadLine()) != null) settings.Add(str); } foreach (var setting in settings) { var separatorIndex = setting.IndexOf(separator); if (separatorIndex == -1) { continue; } string key = setting.Substring(0, separatorIndex).Trim(); string value = setting.Substring(separatorIndex + 1).Trim(); switch (key) { case "DataProvider": shellSettings.DataProvider = value; break; case "DataConnectionString": shellSettings.DataConnectionString = value; break; default: shellSettings.RawDataSettings.Add(key, value); break; } } return shellSettings; }