-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.cs
54 lines (48 loc) · 1.46 KB
/
Settings.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace Settings
{
public class Settings
{
protected static string _fileName = "Settings.xml";
/// <summary>
/// Default constructor provided only for XML Serialization.
/// For instantiation please use Load() instead.
/// </summary>
public Settings() {}
public void Save()
{
string path = Path.Combine(Program.AppDataPath, _fileName);
using (FileStream fs = new FileStream(path, FileMode.Create))
{
XmlSerializer xs = new XmlSerializer(typeof(Settings));
xs.Serialize(fs, this);
}
}
public static Settings Load()
{
string path = Path.Combine(Program.AppDataPath, _fileName);
Settings settingsToReturn;
FileStream fs = null;
try
{
fs = new FileStream(path, FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(Settings));
settingsToReturn = (Settings)xs.Deserialize(fs);
}
catch (FileNotFoundException)
{
settingsToReturn = new Settings();
}
finally
{
if (fs != null)
fs.Close();
}
return settingsToReturn;
}
}
}