-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXML_Utils.cs
More file actions
57 lines (48 loc) · 1.23 KB
/
Copy pathXML_Utils.cs
File metadata and controls
57 lines (48 loc) · 1.23 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.IO;
using System.Xml.Serialization;
namespace SSC
{
public static class XML_Utils
{
public static T Load<T>(string filePath, T defaultVal)
{
string DirectoryPath = Directory.GetParent(filePath).FullName;
if (!Directory.Exists(DirectoryPath))
{
Directory.CreateDirectory(DirectoryPath);
return defaultVal;
}
if (!File.Exists(filePath))
return defaultVal;
FileStream fs = null;
T result = defaultVal;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
fs = new FileStream(filePath, FileMode.Open);
result = (T)serializer.Deserialize(fs);
}
catch (Exception ex)
{
MainForm.Instance.ThreadSafeAddPreviewText($"Error: {ex.Message}", LineType.TwitchSocketCommand);
result = defaultVal;
}
finally
{
fs.Close();
}
return result;
}
public static void Save<T>(string filePath, T objectToSave)
{
string DirectoryPath = Directory.GetParent(filePath).FullName;
if (!Directory.Exists(DirectoryPath))
Directory.CreateDirectory(DirectoryPath);
XmlSerializer serializer = new XmlSerializer(typeof(T));
StreamWriter fw = new StreamWriter(filePath);
serializer.Serialize(fw, objectToSave);
fw.Close();
}
}
}