-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
ConfigurationStore.cs
59 lines (48 loc) · 1.46 KB
/
ConfigurationStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System.IO;
using System.Text;
using nanoFramework.Json;
namespace JsonConfigurationStore
{
public class ConfigurationStore
{
private string _configFile { get; set; }
public ConfigurationStore(string configFile = "I:\\configuration.json")
{
_configFile = configFile;
}
public void ClearConfig()
{
if (File.Exists(_configFile))
{
File.Delete(_configFile);
}
}
public bool IsConfigFileExisting => File.Exists(_configFile);
public Configuration GetConfig()
{
var json = new FileStream(_configFile, FileMode.Open);
Configuration config = (Configuration)JsonConvert.DeserializeObject(json, typeof(Configuration));
return config;
}
public bool WriteConfig(Configuration config)
{
try
{
var configJson = JsonConvert.SerializeObject(config);
var json = new FileStream(_configFile, FileMode.Create);
byte[] buffer = Encoding.UTF8.GetBytes(configJson);
json.Write(buffer, 0, buffer.Length);
json.Dispose();
return true;
}
catch
{
return false;
}
}
}
}