Created
May 24, 2019 07:59
-
-
Save savaged/513b8b8177ce43db3fa466b21fe25827 to your computer and use it in GitHub Desktop.
Local Cached Properties Service (local props ini file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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