-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppConfig.cs
73 lines (68 loc) · 1.75 KB
/
AppConfig.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
62
63
64
65
66
67
68
69
70
71
72
73
namespace VCodeHunt.Config
{
using System;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
public class AppConfigIO
{
private static string m_file = "config.cfg";
public static void Read<T>(ref T config)
{
bool error = false;
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StreamReader reader = File.OpenText(m_file))
{
config = (T)xs.Deserialize(reader);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
error = true;
}
if (error)
{
try
{
File.Delete(m_file);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
public static void Write<T>(T config)
{
bool error = false;
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StreamWriter writer = File.CreateText(m_file))
{
xs.Serialize(writer, config);
writer.Flush();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
error = true;
}
if (error)
{
try
{
File.Delete(m_file);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
}
}