-
Notifications
You must be signed in to change notification settings - Fork 16
/
Settings.cs
92 lines (79 loc) · 2.69 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Globalization;
using System.IO;
using System.Xml;
namespace Weland {
// from http://www.codeproject.com/KB/XML/quickndirtyxml.aspx
public class Settings {
XmlDocument xmlDocument = new XmlDocument();
string applicationData;
string documentPath;
private CultureInfo ic = CultureInfo.InvariantCulture;
public Settings() {
applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "//Weland";
documentPath = applicationData + "//settings.xml";
try {
xmlDocument.Load(documentPath);
}
catch {xmlDocument.LoadXml("<settings></settings>");}
}
public int GetSetting(string xPath, int defaultValue) {
return Convert.ToInt32(GetSetting(xPath, Convert.ToString(defaultValue, ic)), ic);
}
public void PutSetting(string xPath, int value) {
PutSetting(xPath, Convert.ToString(value, ic));
}
public bool GetSetting(string xPath, bool defaultValue) {
return Convert.ToBoolean(GetSetting(xPath, Convert.ToString(defaultValue, ic)), ic);
}
public double GetSetting(string xPath, double defaultValue) {
return Convert.ToDouble(GetSetting(xPath, Convert.ToString(defaultValue, ic)), ic);
}
public void PutSetting(string xPath, double value) {
PutSetting(xPath, Convert.ToString(value, ic));
}
public void PutSetting(string xPath, bool value) {
PutSetting(xPath, Convert.ToString(value, ic));
}
public string GetSetting(string xPath, string defaultValue) {
XmlNode xmlNode = xmlDocument.SelectSingleNode("settings/" + xPath );
if (xmlNode != null) {
return xmlNode.InnerText;
} else {
return defaultValue;
}
}
public void PutSetting(string xPath, string value) {
XmlNode xmlNode = xmlDocument.SelectSingleNode("settings/" + xPath);
if (xmlNode == null) {
xmlNode = createMissingNode("settings/" + xPath);
}
xmlNode.InnerText = value;
try {
if (!Directory.Exists(applicationData)) {
Directory.CreateDirectory(applicationData);
}
xmlDocument.Save(documentPath);
} catch (Exception) {
}
}
private XmlNode createMissingNode(string xPath) {
string[] xPathSections = xPath.Split(new char[] {'/'});
string currentXPath = "";
XmlNode testNode = null;
XmlNode currentNode = xmlDocument.SelectSingleNode("settings");
foreach (string xPathSection in xPathSections) {
currentXPath += xPathSection;
testNode = xmlDocument.SelectSingleNode(currentXPath);
if (testNode == null) {
currentNode.InnerXml += "<" +
xPathSection + "></" +
xPathSection + ">";
}
currentNode = xmlDocument.SelectSingleNode(currentXPath);
currentXPath += "/";
}
return currentNode;
}
}
}