-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFileSettingsStore.cs
More file actions
425 lines (339 loc) · 11.1 KB
/
FileSettingsStore.cs
File metadata and controls
425 lines (339 loc) · 11.1 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
namespace Menees
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Xml;
using System.Xml.Linq;
#endregion
internal sealed class FileSettingsStore : ISettingsStore
{
#region Private Data Members
private readonly XElement root;
#endregion
#region Constructors
public FileSettingsStore()
{
foreach (string fileName in GetPotentialFileNames())
{
// Make sure the file exists and isn't zero length. If a process was killed while saving data,
// then the settings store file may be empty. If so, we'll pretend it isn't there.
FileInfo fileInfo = new(fileName);
if (fileInfo.Exists && fileInfo.Length > 0)
{
try
{
// Only read settings from a file that we have *write* access to so if we're running from a remote read-only file share,
// then it will load a local saved file (e.g., from AppData) instead of loading one from the remote read-only share.
using (FileStream stream = new(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
this.root = XElement.Load(stream);
}
break;
}
catch (XmlException ex)
{
Log.Warning(typeof(FileSettingsStore), $"Unable to load the user settings from {fileName}.", ex);
}
catch (Exception ex) when (Exceptions.IsAccessException(ex))
{
// Ignore the file if we don't have both read and write access to it. If we're running off a read-only share,
// we know Save() can't write back to that location, so we shouldn't read in from that location on startup.
Log.Debug(typeof(FileSettingsStore), $"Unable to get read/write access to {fileName}.", ex);
}
}
}
this.root ??= FileSettingsNode.CreateNodeElement(ApplicationInfo.ApplicationName);
this.RootNode = new FileSettingsNode(this.root, null);
}
#endregion
#region ISettingsStore Members
public ISettingsNode RootNode
{
get;
}
public void Save()
{
bool saved = false;
Dictionary<string, object> errorLogProperties = [];
foreach (string fileName in GetPotentialFileNames())
{
try
{
// Make sure the directory we're trying to save to actually exists.
string? directoryName = Path.GetDirectoryName(fileName);
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(directoryName);
}
if (File.Exists(fileName))
{
// Make sure the file isn't hidden, so XElement.Save can write to it.
File.SetAttributes(fileName, File.GetAttributes(fileName) & ~FileAttributes.Hidden);
}
// Attempt to save the file.
this.root.Save(fileName);
saved = true;
// Hide the file from normal listings since we'll be creating files for each user.
File.SetAttributes(fileName, File.GetAttributes(fileName) | FileAttributes.Hidden);
break;
}
catch (Exception ex) when (Exceptions.IsAccessException(ex))
{
errorLogProperties.Add(fileName, ex);
}
}
if (!saved)
{
throw Exceptions.NewInvalidOperationException("Unable to save the user settings to a file store.", errorLogProperties);
}
}
public void Dispose()
{
// There's nothing to do here since XElement isn't disposable.
}
#endregion
#region Private Methods
private static List<string> GetPotentialFileNames()
{
string fileName = ApplicationInfo.ApplicationName + "-" + Environment.UserDomainName + "-" + Environment.UserName + ".stgx";
List<string> result = [];
// First, try the application's base directory. That gives the best isolation for side-by-side app usage,
// but a non-admin user may not have permissions to write to this directory. Side-by-side isolation is
// important if old and new versions of the software are run on the same machine. (New versions will
// typically be able to read in old settings, but they'll only write out settings in the new format. After
// that "upgrade", the old version wouldn't be able to read the settings in the new format.)
string path = Path.Combine(ApplicationInfo.BaseDirectory, fileName);
result.Add(path);
// Next, try a Menees folder under the user's AppData\Local folder. The user should be able to write
// to their local AppData folder if it exists (e.g., if the user is not in a roaming profile).
string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if (!string.IsNullOrEmpty(localAppDataPath))
{
path = Path.Combine(localAppDataPath, nameof(Menees), fileName);
result.Add(path);
}
// If all else fails, try to use the current temp directory (which may or may not be user-specific);
path = Path.Combine(Path.GetTempPath(), nameof(Menees), fileName);
result.Add(path);
return result;
}
#endregion
#region Private Types
private sealed class FileSettingsNode : ISettingsNode
{
#region Private Data Members
private readonly XElement element;
private readonly FileSettingsNode? parent;
#endregion
#region Constructors
public FileSettingsNode(XElement element, FileSettingsNode? parent)
{
this.element = element;
this.parent = parent;
}
#endregion
#region ISettingsNode Members
public string NodeName
{
get
{
string result = GetNodeName(this.element);
return result;
}
}
public int SettingCount
{
get
{
int result = this.GetSettingElements().Count();
return result;
}
}
public int SubNodeCount
{
get
{
int result = this.GetSubNodeElements().Count();
return result;
}
}
public ISettingsNode? ParentNode => this.parent;
public string GetValue(string settingName, string defaultValue)
=> this.GetValueN(settingName, defaultValue) ?? defaultValue;
public string? GetValueN(string settingName, string? defaultValue)
{
string? result = defaultValue;
XElement? settingElement = this.GetSettingElement(settingName, false);
if (settingElement != null)
{
result = settingElement.GetAttributeValueN("Value", defaultValue);
}
return result;
}
public void SetValue(string settingName, string? value)
{
XElement settingElement = this.GetSettingElement(settingName, true)!;
settingElement.SetAttributeValue("Value", value);
}
public int GetValue(string settingName, int defaultValue)
{
int result = defaultValue;
XElement? settingElement = this.GetSettingElement(settingName, false);
if (settingElement != null)
{
result = settingElement.GetAttributeValue("Value", defaultValue);
}
return result;
}
public void SetValue(string settingName, int value)
{
this.SetValue(settingName, Convert.ToString(value));
}
public bool GetValue(string settingName, bool defaultValue)
{
bool result = defaultValue;
XElement? settingElement = this.GetSettingElement(settingName, false);
if (settingElement != null)
{
result = settingElement.GetAttributeValue("Value", defaultValue);
}
return result;
}
public void SetValue(string settingName, bool value)
{
this.SetValue(settingName, Convert.ToString(value));
}
public T GetValue<T>(string settingName, T defaultValue)
where T : struct
{
T result = defaultValue;
XElement? settingElement = this.GetSettingElement(settingName, false);
if (settingElement != null)
{
result = settingElement.GetAttributeValue("Value", defaultValue);
}
return result;
}
public void SetValue<T>(string settingName, T value)
where T : struct
{
this.SetValue(settingName, Convert.ToString(value));
}
public IList<string> GetSettingNames()
{
var result = this.GetSettingElements().Select(e => GetSettingName(e)).ToList();
return result;
}
public void DeleteSetting(string settingName)
{
XElement? settingElement = this.GetSettingElement(settingName, false);
settingElement?.Remove();
}
public IList<string> GetSubNodeNames()
{
var result = this.GetSubNodeElements().Select(e => GetNodeName(e)).ToList();
return result;
}
public void DeleteSubNode(string nodeNameOrPath)
{
XElement? subElement = this.GetSubNodeElement(nodeNameOrPath, false);
subElement?.Remove();
}
public ISettingsNode GetSubNode(string nodeNameOrPath)
{
XElement subElement = this.GetSubNodeElement(nodeNameOrPath, true)!;
FileSettingsNode result = new(subElement, this);
return result;
}
public ISettingsNode? TryGetSubNode(string nodeNameOrPath)
{
FileSettingsNode? result = null;
XElement? subElement = this.GetSubNodeElement(nodeNameOrPath, false);
if (subElement != null)
{
result = new FileSettingsNode(subElement, this);
}
return result;
}
#endregion
#region Internal Methods
internal static XElement CreateNodeElement(string nodeName)
{
Conditions.RequireString(nodeName, nameof(nodeName));
XElement result = new("Settings", new XAttribute("Name", nodeName));
return result;
}
#endregion
#region Private Methods
private static string GetNodeName(XElement nodeElement)
{
string result = nodeElement.GetAttributeValue("Name");
return result;
}
private static string GetSettingName(XElement settingElement)
{
string result = settingElement.GetAttributeValue("Name");
return result;
}
private static IEnumerable<XElement> GetSubNodeElements(XElement element)
{
var result = element.Elements("Settings");
return result;
}
private IEnumerable<XElement> GetSubNodeElements()
{
var result = GetSubNodeElements(this.element);
return result;
}
private IEnumerable<XElement> GetSettingElements()
{
var result = this.element.Elements("Setting");
return result;
}
private XElement? GetSubNodeElement(string nodeNameOrPath, bool createIfNotFound)
{
Conditions.RequireString(nodeNameOrPath, nameof(nodeNameOrPath));
XElement? result = null;
XElement currentElement = this.element;
// Use backslash as the path separator because that's what RegistryKey.OpenSubKey uses,
// and callers need to be able to switch between the ISettingsStore types without breaking.
// FWIW, RegistryKey ignores the forward slash character, so we can't split on it.
string[] nodeNames = nodeNameOrPath.Split('\\');
foreach (string nodeName in nodeNames)
{
result = GetSubNodeElements(currentElement).SingleOrDefault(e => GetNodeName(e) == nodeName);
if (result == null)
{
if (!createIfNotFound)
{
break;
}
result = CreateNodeElement(nodeName);
currentElement.Add(result);
}
currentElement = result;
}
return result;
}
private XElement? GetSettingElement(string settingName, bool createIfNotFound)
{
XElement? result = this.GetSettingElements().SingleOrDefault(e => GetSettingName(e) == settingName);
if (result == null && createIfNotFound)
{
result = new XElement("Setting", new XAttribute("Name", settingName));
this.element.Add(result);
}
return result;
}
#endregion
}
#endregion
}
}