Skip to content

Instantly share code, notes, and snippets.

@pluveto
Created February 28, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pluveto/536c843b1fd85ce53e82a8ed2a94025f to your computer and use it in GitHub Desktop.
Save pluveto/536c843b1fd85ce53e82a8ed2a94025f to your computer and use it in GitHub Desktop.
Json config manager / helper for c#
/// <summary>
/// 配置文件对象管理器
/// </summary>
public class ConfigManager<T>
{
/// <summary>
/// 缓存到内存的配置文件
/// </summary>
public string FileName { get; private set; }
public T Object;
public ConfigManager(string fileName = "config.json")
{
this.FileName = fileName;
}
public T Load()
{
var json = File.ReadAllText(this.FileName);
this.Object = JsonConvert.DeserializeObject<T>(json);
return this.Object;
}
public void Save()
{
var json = JsonConvert.SerializeObject(Object);
File.WriteAllText(this.FileName, json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment