-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReminderDataPersistenceFileProvider.cs
61 lines (55 loc) · 1.78 KB
/
ReminderDataPersistenceFileProvider.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
60
61
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace Hedgehog.UpdateReminder
{
public class ReminderDataPersistenceFileProvider<T> where T : class, new()
{
public string FilePath { get; set; }
public T LoadData()
{
T data = new T();
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (storage.FileExists(FilePath))
{
using (IsolatedStorageFileStream stream = storage.OpenFile(FilePath, FileMode.Open))
{
XmlSerializer xml = new XmlSerializer(typeof(T));
data = xml.Deserialize(stream) as T;
stream.Close();
}
}
}
return data;
}
public void SaveData(T data)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var fileStream = store.CreateFile(FilePath))
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (var writer = new StreamWriter(fileStream))
{
ser.Serialize(writer, data);
}
}
}
public void EnsureFileExistence(T data)
{
var fileExist = false;
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
fileExist = storage.FileExists(FilePath);
}
if (!fileExist)
{
this.SaveData(data);
}
}
}
}