/// <summary> /// Load settings /// </summary> /// <param name="filePath">File path; pass null to use default settings file path</param> /// <returns></returns> public virtual DataSettings LoadSettings(string filePath = null) { if (String.IsNullOrEmpty(filePath)) { filePath = Path.Combine(CommonHelper.MapPath("~/Settings/"), filename); //throw new Exception(filePath + "hung"); } if (File.Exists(filePath)) { string text = File.ReadAllText(filePath); CacheSettings = ParseSettings(text); return(CacheSettings); } //"name=AppEntities"; CacheSettings = new DataSettings(); return(CacheSettings); }
public static void Delete() { if (s_current?.TenantPath != null) { using (s_rwLock.GetWriteLock()) { string filePath = Path.Combine(CommonHelper.MapPath(s_current.TenantPath), FILENAME); try { File.Delete(filePath); } finally { s_current = null; s_installed = null; } } } }
/// <summary> /// Save settings to a file /// </summary> /// <param name="settings"></param> public virtual void SaveSettings(DataSettings settings) { if (settings == null) { throw new ArgumentNullException("settings"); } string filePath = Path.Combine(CommonHelper.MapPath("~/"), 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> /// Load data settings /// </summary> /// <param name="filePath">File path; pass null to use the default settings file</param> /// <param name="reloadSettings">Whether to reload data, if they already loaded</param> /// <param name="fileProvider">File provider</param> /// <returns>Data settings</returns> public static DataSettings LoadSettings(string filePath = null, bool reloadSettings = false, INopFileProvider fileProvider = null) { if (!reloadSettings && Singleton <DataSettings> .Instance != null) { return(Singleton <DataSettings> .Instance); } fileProvider = fileProvider ?? CommonHelper.DefaultFileProvider; filePath = filePath ?? fileProvider.MapPath(NopDataSettingsDefaults.FilePath); //check whether file exists if (!fileProvider.FileExists(filePath)) { //if not, try to parse the file that was used in previous nopCommerce versions filePath = fileProvider.MapPath(NopDataSettingsDefaults.ObsoleteFilePath); if (!fileProvider.FileExists(filePath)) { return(new DataSettings()); } //get data settings from the old txt file var dataSettings = new DataSettings(); using (var reader = new StringReader(fileProvider.ReadAllText(filePath, Encoding.UTF8))) { string settingsLine; while ((settingsLine = reader.ReadLine()) != null) { var separatorIndex = settingsLine.IndexOf(':'); if (separatorIndex == -1) { continue; } var key = settingsLine.Substring(0, separatorIndex).Trim(); var value = settingsLine.Substring(separatorIndex + 1).Trim(); switch (key) { case "DataProvider": dataSettings.DataProvider = Enum.TryParse(value, true, out DataProviderType providerType) ? providerType : DataProviderType.Unknown; continue; case "DataConnectionString": dataSettings.DataConnectionString = value; continue; default: dataSettings.RawDataSettings.Add(key, value); continue; } } } //save data settings to the new file SaveSettings(dataSettings, fileProvider); //and delete the old one fileProvider.DeleteFile(filePath); Singleton <DataSettings> .Instance = dataSettings; return(Singleton <DataSettings> .Instance); } var text = fileProvider.ReadAllText(filePath, Encoding.UTF8); if (string.IsNullOrEmpty(text)) { return(new DataSettings()); } //get data settings from the JSON file Singleton <DataSettings> .Instance = JsonConvert.DeserializeObject <DataSettings>(text); return(Singleton <DataSettings> .Instance); }