0% found this document useful (0 votes)
3 views

Config

The document defines a singleton class 'Config' in C# for managing configuration settings, including properties for first-time setup and a collection of added files. It includes methods for saving and loading configuration data to and from a JSON file. Error handling is implemented to ensure the configuration can be reset if loading fails.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Config

The document defines a singleton class 'Config' in C# for managing configuration settings, including properties for first-time setup and a collection of added files. It includes methods for saving and loading configuration data to and from a JSON file. Error handling is implemented to ensure the configuration can be reset if loading fails.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System.

IO;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace DeathSounds.code
{
public class Config
{
public bool firstEverEnter = true;
public bool doneFirstTimeSetup = false;
public bool doSecondReload = false;

public HashSet<string> addedFiles = new HashSet<string>();

private Config()
{
addedFiles = new HashSet<string>();
firstEverEnter = true;
doneFirstTimeSetup = false;
doSecondReload = false;
}
private static Config instance;
public static Config Instance
{
get
{
if (instance == null)
instance = new Config();
return instance;
}
private set { instance = value; }
}
public string Save(string path = "")
{
if (String.IsNullOrEmpty(path))
path = PATH.CONFIG_FILE;
string json = JsonConvert.SerializeObject(Instance,
Formatting.Indented);
File.WriteAllText(path, json);
return json;
}
public string Load(string path = "")
{
if (String.IsNullOrEmpty(path))
path = PATH.CONFIG_FILE;
string json = "";
try
{
json = File.ReadAllText(path);
JsonConvert.PopulateObject(json, Config.Instance);
}
catch
{
Config.Instance = null;
json = JsonConvert.SerializeObject(Config.Instance);
Directory.CreateDirectory(Directory.GetParent(path).FullName);
File.WriteAllText(path, json);
}
return json;
}
}
}

You might also like