/// <summary> /// Reads the settings from file /// </summary> /// <returns>Returns false if failed</returns> static private bool ReadSettings() { /*Load settings if file exists, else create one*/ string filePath = Path.Combine(Application.StartupPath, "Settings.json"); if (!File.Exists(filePath)) { /*Set up settings class to print*/ Config.Settings settings = new Config.Settings(); /*Add example accounts*/ var tempacc = new Config.AccountInfo(); tempacc.SetTemporaryValues(); settings.Account.Add(tempacc); settings.Account.Add(tempacc); settings.Account.Add(tempacc); /*Write settings to file*/ string settingsJson = JsonConvert.SerializeObject(settings, Formatting.Indented); File.WriteAllText(filePath, settingsJson); Console.WriteLine("Settings.json has been written. Edit it for your accounts."); Thread.Sleep(1500); } else { /*Enable missing objects error*/ JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); jsonSettings.MissingMemberHandling = MissingMemberHandling.Error; string settingsJson = string.Empty; try { /*Load the settings from file*/ settingsJson = File.ReadAllText(filePath); mSettings = JsonConvert.DeserializeObject <Config.Settings>(settingsJson, jsonSettings); /*Write the class to settins file incase some of the settings were missing from the file originally*/ File.WriteAllText(filePath, JsonConvert.SerializeObject(mSettings, Formatting.Indented)); return(true); } catch (JsonReaderException ex) { /*There was an error parsing the json file, most likely due to user trying to manually edit it without knowing the syntax*/ Console.WriteLine("Error: The settings file is corrupt. Please delete it and restart the program."); Console.WriteLine($"{ex.Message}\n\n"); } catch (Exception ex) { /*I wonder what happened here?*/ Console.WriteLine("Error: An unhandled exception occured when parsing the settings? What the hell?"); Console.WriteLine($"{ex.Message}\n\n"); } } Console.WriteLine("Exiting in 10 seconds..."); Thread.Sleep(10000); return(false); }
/// <summary> /// Reads the settings from file /// </summary> /// <returns>Returns false if failed</returns> static private bool ReadSettings() { /*Load settings if file exists, else create one*/ string filePath = Path.Combine(Application.StartupPath, "Settings.json"); if (!File.Exists(filePath)) { /*Set up settings class to print*/ Config.Settings settings = new Config.Settings(); /*Add example accounts*/ var tempacc = new Config.AccountInfo(); tempacc.SetTemporaryValues(); settings.Account.Add(tempacc); settings.Account.Add(tempacc); settings.Account.Add(tempacc); /*Write settings to file*/ string settingsJson = JsonConvert.SerializeObject(settings, Formatting.Indented); File.WriteAllText(filePath, settingsJson); Console.WriteLine("Settings.json has been written. Edit it for your accounts."); Thread.Sleep(1500); } else { try { /*Load the settings from file*/ string settingsJson = File.ReadAllText(filePath); mSettings = JsonConvert.DeserializeObject <Config.Settings>(settingsJson); return(true); } catch (JsonException jex) { /*User f****d up with the formatting probably*/ MessageBox.Show("There was an error parsing Settings.json\n" + "It's either incorrectly formatted or corrupt.\n" + "Delete the file and let the app make a new one.\n\nError: " + jex.Message); } } return(false); }