private void ParseProperties() { // initialize with defaults PropertiesFileContent content = new PropertiesFileContent(); // read properties file content if (File.Exists(_ConfigFilepath)) { try { string json = ""; using (FileStream fs = new FileStream(_ConfigFilepath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using StreamReader sr = new StreamReader(fs); json = sr.ReadToEnd(); } PropertiesFileContent parsedContent = JsonSerializer.Deserialize <PropertiesFileContent>(json); content = parsedContent; } catch (Exception e) { Log.Error($"An error occurred while reading properties from {_ConfigFilepath}: {e.Message}"); } } else { WriteProperties(content); } // apply properties from content CholoateyLogFolder = content.ChocolateyLogs; CleanShortcuts = content.CleanShortcuts; ValidExitCodes = content.ValidExitCodes; }
// --------------------------------------------------------- public void SaveProperties() { PropertiesFileContent content = new PropertiesFileContent { ChocolateyLogs = this.CholoateyLogFolder, CleanShortcuts = this.CleanShortcuts, ValidExitCodes = this.ValidExitCodes }; WriteProperties(content); }
private void WriteProperties(PropertiesFileContent content) { try { JsonSerializerOptions options = new JsonSerializerOptions() { WriteIndented = true }; string json = JsonSerializer.Serialize(content, options); File.WriteAllText(_ConfigFilepath, json); } catch (Exception e) { Log.Error($"An error occurred while saving properties to {_ConfigFilepath}: {e.Message}"); } }