/// <summary> /// Read the settings file /// </summary> /// <returns></returns> public bool Read() { IoC.Logger.Log($"Reading ini file to {Filename}..."); try { SettingsClassInstance.FromIniFile(); } catch (Exception ex) { HandleExceptions(this, ex); return(false); } IoC.Logger.Log($"Ini file read."); return(true); }
/// <summary> /// Save the settings file /// </summary> /// <returns></returns> public bool Save() { IoC.Logger.Log($"Saving ini file to {Filename}..."); try { SettingsClassInstance.ToIniFile(); } catch (Exception ex) { HandleExceptions(this, ex); return(false); } IoC.Logger.Log($"Ini file saved."); return(true); }
/// <summary> /// Read the settings file /// </summary> /// <returns></returns> public bool Read() { // Check if the file exist if (!File.Exists(Filename)) { HandleExceptions(this, new Exception($"The JSON file: {Filename} does not exists.")); return(false); } try { // Read the file in as text IoC.Logger.Log($"Read {Filename} in text..."); var read = File.ReadAllText(Filename); // If the schema exists, validate the json file with the schema if (Schema != null) { try { if (Schema != string.Empty) { // Parse the string to json schema IoC.Logger.Log($"Parsing the schema..."); } JSchema schema = JSchema.Parse(Schema); // Parse the text to json object IoC.Logger.Log($"Parsing the {Filename} to JSON object..."); JObject json = JObject.Parse(read); IoC.Logger.Log($"Validating the JSON file..."); // Validate the json if (!json.IsValid(schema)) { IoC.Logger.Log($"The JSON file: {Filename} does not match the schema."); ErrorOccurs?.Invoke((this, new Exception($"The JSON file: {Filename} does not match the schema."))); // If it's not valid return(false); } IoC.Logger.Log($"Validation Complete."); } catch (Exception ex) { HandleExceptions(this, ex); } } // Deserialize the json object IoC.Logger.Log($"Deserializing JSON format..."); object deserializedObj = JsonConvert.DeserializeObject(read, SettingsClassInstance.GetType()); if (deserializedObj == null) { HandleExceptions(this, new Exception("Cannot deserialize the object.")); return(false); } SettingsClassInstance = (TClass)deserializedObj; } catch (Exception ex) { HandleExceptions(this, ex); return(false); } IoC.Logger.Log($"Read Json finished. Here's the json file that has been read:\r\n" + $"{JsonConvert.SerializeObject(SettingsClassInstance, Formatting.Indented)}"); return(true); }