Skip to content

Instantly share code, notes, and snippets.

@savaged
Created May 24, 2019 07:59
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 savaged/513b8b8177ce43db3fa466b21fe25827 to your computer and use it in GitHub Desktop.
Save savaged/513b8b8177ce43db3fa466b21fe25827 to your computer and use it in GitHub Desktop.
Local Cached Properties Service (local props ini file)
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
namespace AssureDesktop.Data
{
public class LocalCachedPropertiesService
: ILocalCachedPropertiesService
{
private const string _fileLocation = "LocalCachedProperties.ini";
private readonly IDictionary<string, object> _props;
public LocalCachedPropertiesService()
{
var fileInfo = new FileInfo(_fileLocation);
if (fileInfo.Exists)
{
_props = JsonConvert
.DeserializeObject<IDictionary<string, object>>(
File.ReadAllText(_fileLocation));
}
else
{
_props = new Dictionary<string, object>();
}
}
public object Get(string key)
{
if (_props.Keys.Contains(key))
{
return _props[key];
}
return null;
}
public void Set(string key, object value)
{
if (_props.Keys.Contains(key))
{
_props[key] = value;
}
else
{
_props.Add(key, value);
}
var fileInfo = new FileInfo(_fileLocation);
if (fileInfo.Exists)
{
File.Delete(_fileLocation);
}
var json = JsonConvert.SerializeObject(_props);
File.WriteAllText(_fileLocation, json);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment