-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStorage.cs
More file actions
41 lines (37 loc) · 1.05 KB
/
DataStorage.cs
File metadata and controls
41 lines (37 loc) · 1.05 KB
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
using System.IO;
using Newtonsoft.Json;
namespace AuthMe
{
public class DataStorage<T> where T : class
{
public string DataPath { get; private set; }
public DataStorage(string dir, string fileName)
{
DataPath = Path.Combine(dir, fileName);
}
public void Save(T obj)
{
string objData = JsonConvert.SerializeObject(obj, Formatting.Indented);
using (StreamWriter stream = new StreamWriter(DataPath, false))
{
stream.Write(objData);
}
}
public T Read()
{
if (File.Exists(DataPath))
{
string dataText;
using (StreamReader stream = File.OpenText(DataPath))
{
dataText = stream.ReadToEnd();
}
return JsonConvert.DeserializeObject<T>(dataText);
}
else
{
return null;
}
}
}
}