-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPortableSettingsProvider.cs
140 lines (116 loc) · 3.71 KB
/
PortableSettingsProvider.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using iRacingSDK.Support;
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Windows.Forms;
using System.Xml;
public class IAVMSettingsProvider : PortableSettingsProvider
{
public override string Name
{
get { return "IAVMSettingsProvider"; }
}
public override string GetAppSettingsFilename()
{
return "iracing-application-version-manager.settings";
}
}
public class PortableSettingsProvider : SettingsProvider
{
const string SETTINGSROOT = "Settings";
XmlDocument _settingsXML = null;
public override void Initialize(string name, NameValueCollection col)
{
base.Initialize(ApplicationName, col);
}
public override string ApplicationName
{
get { return Application.ProductName; }
set { }
}
public override string Name
{
get { return "PortableSettingsProvider"; }
}
public virtual string GetAppSettingsPath()
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
public virtual string GetAppSettingsFilename()
{
return "iracing-replay-director.settings";
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propvals)
{
foreach (SettingsPropertyValue propval in propvals)
SetValue(propval);
SettingsXML.Save(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
{
var values = new SettingsPropertyValueCollection();
foreach (SettingsProperty setting in props)
{
var value = new SettingsPropertyValue(setting);
value.IsDirty = false;
value.SerializedValue = GetValue(setting);
values.Add(value);
}
return values;
}
XmlDocument SettingsXML
{
get
{
if (_settingsXML != null)
return _settingsXML;
_settingsXML = new XmlDocument();
try
{
_settingsXML.Load(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
}
catch (Exception ex)
{
TraceDebug.WriteLine("Unable to load settings");
TraceDebug.WriteLine(ex.Message);
var dec = _settingsXML.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
_settingsXML.AppendChild(dec);
var nodeRoot = _settingsXML.CreateNode(XmlNodeType.Element, SETTINGSROOT, "");
_settingsXML.AppendChild(nodeRoot);
}
return _settingsXML;
}
}
string GetValue(SettingsProperty setting)
{
string ret = "";
try
{
ret = SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + setting.Name).InnerText;
}
catch (Exception)
{
if (setting.DefaultValue != null)
ret = setting.DefaultValue.ToString();
}
return ret;
}
void SetValue(SettingsPropertyValue propVal)
{
XmlElement SettingNode = null;
try
{
SettingNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + propVal.Name);
}
catch
{
}
if (SettingNode == null)
{
SettingNode = SettingsXML.CreateElement(propVal.Name);
SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(SettingNode);
}
SettingNode.InnerText = propVal.SerializedValue == null ? null : propVal.SerializedValue.ToString();
}
}