-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsXpress.cs
3450 lines (3127 loc) · 98.6 KB
/
SettingsXpress.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region License and Copyright
/* ------------------------------------------------------------------------------
* SettingsXpress v1.1.00.00
*
* Copyright (c) 2003 Amber Alpha Star All rights reserved.
*
*
* SettingsXp Library provides a simple to use,
* flexible, dynamic, and universal, XML based data store
* for your application and user settings.
*
* File: SettingsXpress.cs
* Created : 5/8/2003 6:58:42 PM by : Amber
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The use and distribution terms for this software are contained in the file
* named license.txt, which can be found in the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by the
* terms of this license.
*
* You must not remove this notice, or any other, from this software.
*
* ------------------------------------------------------------------------------
*/
#endregion
#region Acknowledgements
#endregion
#region Contact Info
/* ------------------------------------------------------------------------------
*
* Contact Information
*
* SettingsXPress is managed in a GotDotNet Workspace, which provides
* a central managment space for the library and it's contributors.
* You can participate in the workspace, post comments, suggestions, fixes, bugs,
* and find New releases.
* To Participate in this workspace follow the WorkSpace link.
*
* WorkSpace: http://www.gotdotnet.com/community/workspaces/workspace.aspx?id=cd9f2d7e-77bf-4231-b741-41b862f7c587
* Comments : mailto:AmberAlphaStar@hotmail.com
*
* --------------------------------------------------------------------------------
*/
#endregion
#region Release Notes
/*-----------------------------------------------------------------------------------------------------
* Release Information.
*-----------------------------------------------------------------------------------------------------
* 04/20/2003 - XmlRegistry Version: 0.5.00.00 Released
*
*/
#endregion
#region Todo List
//TODO: Verify all settingKeyNames and settingNames are valid XML Names, and handle invalid names.
#endregion
/*
* The following references are required to build this Library
* System
* System.Data
* System.Windows Forms
* System.Xml
*/
using System;
using System.Xml;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Permissions;
namespace Nastavitve
{
#region SettingsFile
/// <summary>
/// Provides access to the settings file and the root <see cref="SettingsKey"/> in the settings file.
/// <seealso cref="SettingsKey"/>
/// </summary>
///
///
/// <remarks>
///The SettingsFile acts as a central storage for your
///application and component settings.
///
///<para>
///The settings file is organized in a hierarchical format, based on a logical
///ordering of <see cref="SettingsKey">SettingsKeys</see> stored within it
///</para>
///
///
/// For example, applications can use the SettingsFile
/// for storing information that needs to be preserved once the application is closed,
/// and access that same information when the application is reloaded.
/// For instance, you can store color preferences, screen locations,
/// or the size of the window.
///
///<para>
///A <see cref="SettingsKey"/> is the base unit of organization in the settings file, and
///can be compared to folders.
///</para>
///
///<para>
///Each <see cref="SettingsKey"/> can have multiple settings stored in it
///(a setting can be compared to a file within a folder),
///SettingsKeys are used to store the actual settings of your application components, and users.
///Each setting holds one particular value, which can be
///retrieved or updated when required.
///</para>
///
/// <para>
/// Use the <see cref="Settings"/> property to access
/// the root <see cref="SettingsKey"/> in the settings file. The root SettingsKey is readonly
/// since the settings file depends on it's existence.
/// </para>
///
/// <para>
/// By default the settings file is saved to the current users appData path with the
/// default name 'Settings.Xml'. To specify a diferent name and location use the
/// <see cref="Create"/> method.
/// </para>
///
/// <para>Once you have made changes to the settings file use the <see cref="Update"/>
/// method to save your changes to the file.
/// </para>
/// </remarks>
///
public sealed class SettingsFile
{
#region Constants
const string RES_NullParameter = "The specified string parameter is null or empty.";
const string RES_XmlFormat = "Could not Parse or Load the SettingsFile";
const string RES_RootName = "Settings";
const string RES_FileName = "Settings.xml";
#endregion
#region Class Fields
static System.Xml.XmlDocument data;
private static SettingsKey root;
static bool isOpen = false;
static bool hasChanges = false;
static string fileName;
#endregion
#region Class Constructors
private SettingsFile()
{
}
static SettingsFile()
{
string path = System.Windows.Forms.Application.UserAppDataPath;
fileName = Path.Combine(path,RES_FileName);
}
#endregion
#region Private Class Methods
#region open
//open
private static void open()
{
//is parameter fileName null or empty
if (fileName == null | fileName == string.Empty)
{
throwParameterNullException("fileName");
}
data = new XmlDocument();
// if file exsist load, if not create
if (File.Exists(fileName))
{
try
{
data.Load(fileName);
}
catch(XmlException ex)
{
throwXmlFormatException(ex);
}
}
else
{
data.AppendChild(data.CreateElement(RES_RootName));
root = new SettingsKey(data.DocumentElement,true);
}
//set rootkey to readonly
root = new SettingsKey(data.DocumentElement,true);
//set handlers
XmlNodeChangedEventHandler handler = new XmlNodeChangedEventHandler(handleChanges);
data.NodeChanged += handler;
data.NodeInserted += handler;
data.NodeRemoved += handler;
//set dirty flag
hasChanges = false;
//set Open
isOpen = true;
}
#endregion
#region update
//update
internal static void update()
{
if(hasChanges)
{
data.Save(fileName);
}
hasChanges = false;
}
#endregion
#region exceptions
private static void throwParameterNullException(string parameterName)
{
throw new ArgumentNullException(parameterName, RES_NullParameter);
}
private static void throwXmlFormatException(XmlException internalException)
{
string message = string.Concat(RES_XmlFormat,fileName);
throw new SettingsFormatException(message,internalException.LineNumber,internalException.LinePosition,internalException);
}
#endregion
#region handleChanges
//handles the XmlDocument Change events
private static void handleChanges(object sender, XmlNodeChangedEventArgs e)
{
hasChanges = true;
}
#endregion
#endregion
#region Public Class Methods
#region Create
/// <summary>
/// Creates or loads the specified settings file
/// </summary>
///
/// <param name="fileName">
/// The name of the settings file to load.
/// </param>
///
/// <remarks>
///Use this method if you want to specify a specific file name and location,
///for the settings file. If the specified file exists it is loaded, otherwise
///it is created.
///</remarks>
public static void Create(string fileName)
{
SettingsFile.fileName = fileName;
open();
}
/// <summary>
/// Creates or loads the settings file using the default settings.
/// </summary>
public static void Create()
{
string path = System.Windows.Forms.Application.UserAppDataPath;
fileName = Path.Combine(path,RES_FileName);
}
#endregion
#region Update
//Update
/// <summary>
/// Updates the settings file with any changes made.
/// </summary>
///
/// <remarks>
/// It is the clients responsibility to ensure that the
/// settings file is available for write access
/// if the settings cannot be updated
/// an IOException will be thrown, and the client can recover
/// from the exception using the WriteTo method
/// </remarks>
public static void Update()
{
update();
}
#endregion
#region WriteTo
//WriteTo
/// <summary>
/// Writes the SettingsFile content to the specified XmlWriter
/// </summary>
/// <param name="w">
/// The XmlWriter to which you want to write to.
/// </param>
public static void WriteTo(XmlWriter w)
{
data.WriteTo(w);
}
#endregion
#endregion
#region Class Properties
#region Settings
//Settings
/// <summary>
/// The root <see cref="SettingsKey"/> in the setting file.
/// <seealso cref="SettingsKey"/>
/// </summary>
/// <example>
/// The following example method creates a sub SettingKey using the root SettingsKey
/// <code>
///
/// private SettingsKey GetSettingsKey()
/// {
/// SettingsKey dataSettings = SettingsFile.Settings.CreateSubKey("Data");
/// return dataSettings;
/// }
/// </code>
/// </example>
public static SettingsKey Settings
{
get
{
if(!isOpen)
{
open();
}
return root;
}
}
#endregion
#endregion
}
#endregion
#region SettingsKey
/// <summary>
/// Represents a settings section in the <see cref="SettingsFile"/>.
/// </summary>
/// <remarks>
///<para>
///The settings file is organized in a hierarchical format, based on a logical
///ordering of the SettingsKeys stored within it
///</para>
///
///<para>
///A SettingsKey is the base unit of organization in the settings file, and
///can be compared to a folder to store settings in. A particular SettingsKey
///can have subkeys (just as a folder can have subfolders). SettingsKeys can be
///added and deleted to the settings file.
///</para>
///
///<para>
///Each SettingsKey can have multiple settings stored within it,
///Each setting holds one particular value, which can be
///retrieved or updated when required.
///</para>
///
///<para>
///When storing information in the settings file, select the appropriate
///location based upon the type of information being stored and be sure
///to avoid destroying settings created by other components in your
///application, since this can cause those components to exhibit unexpected
///behavior, and can also have an adverse effect upon your application.
///</para>
///
///<para>
///NOTE: All SettingsKey and Setting Names must conform to W3C Extended Markup Language recommendation
///</para>
///
/// </remarks>
public sealed class SettingsKey: System.IDisposable
{
#region Constants
const string RES_NullParameter = "The specified string parameter is null or empty.";
const string RES_ReadOnlyKey = "The current SettingsKey is the Root SettingsKey or read-only.";
const string RES_InvalidKeyRef = "The specified paramter is not a valid reference to a SettingsKey.";
const string RES_InvalidValueRef = "The specified paramter is not a valid reference to a setting.";
const string RES_InvalidDeleteWithChildKeys = "The specified SettingsKey has subkeys.Cannot delete a SettingsKey with subkeys. To delete a SettingsKey that has subkeys use the DeleteSettingsKeyTree method.";
const string RES_PointStartString = "{X=";
const string RES_PointEndString = "Y=";
const char RES_PointDelimChar = ',';
const char RES_PointEndChar = '}';
const string RES_SizeStartString = "{Width=";
const string RES_SizeEndString = " Height=";
const char RES_SizeDelimChar = ',';
const char RES_SizeEndChar = '}';
const string RES_ColorParseError = "Color could not be parsed";
const string RES_KnownColorStartString = "COLOR[";
const string RES_UnKnownColorStartString = "RGB[";
const char RES_ColorEndChar = ']';
#endregion
#region Instance Fields
private XmlElement data;
internal bool isReadOnly = true;
#endregion
#region Instance Constructors
internal SettingsKey(XmlElement data, bool isReadOnly)
{
this.data = data;
this.isReadOnly = isReadOnly;
}
private SettingsKey()
{
}
#endregion
#region Indexers
/// <summary>
/// Returns the specified subkey
/// You can specify a name of a subkey or a relative path
/// to a subkey. In C# this is the Indexer
/// </summary>
public SettingsKey this[string path]
{
get
{
return this.CreateSubKey(path);
}
}
#endregion
#region Public Instance Properties
#region Name
//FullName
/// <summary>
/// The name including the absolute path of the SettingsKey.
/// </summary>
/// <remarks>
/// The path of the SettingsKey always starts at a Root SettingsKey
/// </remarks>
public string FullName
{
get
{
string str;
// if i am root return name
if (data == SettingsFile.Settings.data)
{
str = String.Concat("/",data.Name);
}
else
{
// this occurs when key has been deleted(Orphan Key)
if((XmlElement)data.ParentNode == null)
{
return this.data.Name;
}
//recursively call Name on parent until we find Root.
SettingsKey parentKey = new SettingsKey((XmlElement)data.ParentNode, true);
string parentName = parentKey.FullName;
str = String.Concat(parentName, "/", data.Name);
}
return str;
}
}
//Name
/// <summary>
/// The name of the SettingsKey.
/// </summary>
public string Name
{
get
{
return this.data.Name;
}
}
#endregion
#region SubKeyCount
//SubKeyCount
/// <summary>
/// Retrieves the count of subkeys at the base level,
/// for the current key.
/// </summary>
///
/// <setting>
/// The number of subkeys for the current key.
/// </setting>
///
/// <remarks>
/// This property does not recursively count names.
/// It only returns the count of names on the base
/// level from which it was called.
/// </remarks>
public int SubKeyCount
{
get
{
return data.ChildNodes.Count;
}
}
#endregion
#region SettingsCount
//SettingsCount
/// <summary>
/// Retrieves the count of settings stored in the key.
/// </summary>
/// <setting>
/// A count of the number of settings stored in the key.
/// </setting>
public int SettingsCount
{
get
{
return data.Attributes.Count;
}
}
#endregion
#endregion
#region Public Instance Methods
#region CreateSubkey
//CreateSubKey
/// <summary>
/// Creates a new subkey or opens an existing subkey.
/// </summary>
///
/// <param name="subkeyName">
/// Name or the path of subkey to create or open.
/// </param>
///
/// <returns>
/// Returns the SettingsKey, with read-write access.
/// Returns null if the operation failed.
/// </returns>
///
/// <exception cref="ArgumentNullException">
/// The specified parameter string 'subkeyName' is null or an empty string.
/// </exception>
///
///<remarks>
///If an invalid character is used in any part of the specified
///subKey name or path the method returns a null reference.
///If the subkey specified already exsist, this method will
///return the subkey with read-write access. If the specified
///subkey does not exsist this method will create it and open
///it for read-write access.
///</remarks>
public SettingsKey CreateSubKey(string subkeyName)
{
//trim input
subkeyName = subkeyName.Trim();
subkeyName = subkeyName.TrimStart(new char[]{'/'});
subkeyName = subkeyName.TrimEnd(new char[]{'/'});
//store state if operation fails
string xmlData = this.data.InnerXml ;
#region CheckConditions
//is subkey name null or an empty string
if (subkeyName == null || subkeyName == String.Empty)
{
throwParameterNullException("subkeyName");
}
#endregion
//split input path
string[] strs1 = subkeyName.Split(new char[]{'/'});
XmlElement xmlElement = data;
XmlElement xmlElement2;
int index = 0;
//find all exsisting nodes in path
while((xmlElement2 = xmlElement[(strs1[index].Trim())]) != null)
{
xmlElement = xmlElement2;
index++;
if (index ==(int)strs1.Length)
{
return new SettingsKey(xmlElement, false);
}
}
//iterate strings in path and create if neccasary
for (int i = index; i < (int)strs1.Length; i++)
{
try
{
//once we've created one we know every other is new
xmlElement = (XmlElement)xmlElement.AppendChild(xmlElement.OwnerDocument.CreateElement((strs1[i].Trim())));
}
catch
{
//operation failed restore state
this.data.InnerXml = xmlData;
return null;
}
}
return new SettingsKey(xmlElement, false);
}
#endregion
#region DeleteSubKey
//DeleteSubKey
/// <summary>
/// Deletes the specified subkey.
/// </summary>
///
/// <param name="subkeyName">
/// The name of the subkey to delete.
/// </param>
///
/// <exception cref="UnauthorizedAccessException">
/// The current key is root SettingsKey or is read-only.
/// </exception>
///
/// <exception cref="InvalidOperationException">
/// The subkey specified has child subkeys.
/// To delete a subkey that has child subkeys
/// use DeleteSettingsKeyTree method.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The specified parameter 'subkeyName' is not a valid
/// reference to a SettingsKey.
/// </exception>
///
/// <exception cref="ArgumentNullException">
/// The paramter string 'subkey' is null or an
/// empty string (Nothing in Visual Basic).
/// </exception>
///
/// <remarks>
/// The string parameter 'subkeyName' is case-sensitive.
/// This method can only be used on a key that has write access.
/// To delete child subkeys, use DeleteSettingsKeyTree.
/// Use caution when deleting keys
/// </remarks>
public void DeleteSubKey(string subkeyName)
{
//All conditions met delete subkey
DeleteSubKey(subkeyName,true);
}
/// <summary>
/// Deletes the specified subkey, and optionally raises an exception
/// if the specified subkey is missing.
/// </summary>
///
/// <param name="subkeyName">
/// The name of the subkey to delete.
/// </param>
///
/// <param name="throwOnMissingSubkey">
/// Indicates whether an exception should be raised if the
/// specified subkey cannot be found. If this argument is set
/// to true and the specified subkey does not exist,
/// then an exception is raised. If this argument is set to false
/// and the specified subkey does not exist, then no action is taken.
/// </param>
///
/// <exception cref="UnauthorizedAccessException">
/// The current key is a base(root) key or is read-only.
/// </exception>
///
/// <exception cref="InvalidOperationException">
/// The subkey to delete has subkeys,to delete a subkey
/// that has subkeys use the DeleteSettingsKeyTree method.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The specified subkey is not a valid reference to a SettingsKey
/// and the throwOnMissingSubKey parameter is set to true.
/// </exception>
///
/// <exception cref="ArgumentNullException">
/// The specified parameter string 'subkeyName' is a null reference or
/// an empty string (Nothing in Visual Basic).
/// </exception>
///
/// <remarks>
/// The string 'subkeyName' is case-sensitive.
/// This method can only be used on a SettingsKey opened with write access.
/// To delete child subkeys, use the DeleteSettingsKeyTree method.
/// Use caution when deleting settings keys.
/// </remarks>
public void DeleteSubKey(string subkeyName, bool throwOnMissingSubkey)
{
this.internalDeleteSubKey(subkeyName,throwOnMissingSubkey,false);
}
#endregion
#region DeleteSettingsKeyTree
//DeleteSettingsKeyTree
/// <summary>
/// Deletes a subkey and all child subkeys within the subkey recursively.
/// </summary>
///
/// <param name="subkeyName">
/// The SettingsKey to delete.
/// </param>
///
/// <exception cref="ArgumentNullException">
/// The specified pararameter string 'subkey'
/// is a null reference or an empty string.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The subkey parameter does not match a valid SettingsKey.
/// </exception>
///
/// <exception cref="UnauthorizedAccessException">
/// The current key is the root SettingsKey or is read-only.
/// </exception>
///
/// <remarks>
/// CAUTION:Deleting a particular key will remove all
/// entries below the key in the tree. No warning will be provided.
/// If you want to delete a subkey only when it has no child subkeys,
/// use the DeleteSubKey method instead.
/// <para>
/// The string 'subkeyName' is case-sensitive.
/// </para>
/// </remarks>
public void DeleteSettingsKeyTree(string subkeyName)
{
this.internalDeleteSubKey(subkeyName,true,true);
}
#endregion
#region DeleteSetting
//DeleteSetting
/// <summary>
/// Deletes the specified setting stored in this key.
/// </summary>
///
/// <param name="settingName">
/// The name of the setting to delete.
/// </param>
///
/// <exception cref="UnauthorizedAccessException">
/// This SettingsKey is root SettingsKey or is read-only.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The specified setting name is not a valid reference
/// to a setting stored in this SettingsKey.
/// </exception>
///
/// <exception cref="ArgumentNullException">
/// The specified parameter string 'settingName'
/// is a null reference or an empty string.
/// </exception>
///
/// <remarks>
/// The string 'settingName' is case-sensitive.
/// </remarks>
public void DeleteSetting(string settingName)
{
this.DeleteSetting(settingName,true);
}
/// <summary>
/// Deletes the specified setting from a SettingsKey, and optionally
/// raises an exception if the setting does not exsist.
/// </summary>
///
/// <param name="settingName">
/// The name of the setting to delete.
/// </param>
///
/// <param name="throwOnMissingValue">
/// Indicates whether an exception should be raised if the specified
/// setting cannot be found. If this argument is set to true
/// and the specified setting does not exist then an exception is raised.
/// If this argument is set to false and the specified setting does not exist,
/// then no action is taken.
/// </param>
///
/// <exception cref="UnauthorizedAccessException">
/// The current key is the root SettingsKey or is read-only.
/// </exception>
///
/// <exception cref="ArgumentException">
/// The specified setting name is not a valid reference to a setting
/// stored in the current SettingsKey, and throwOnMissing is set to true.
/// </exception>
///
/// <exception cref="ArgumentNullException">
/// The parameter string 'name' is a null reference or an empty string.
/// </exception>
///
/// <remarks>
/// If throwOnMissingValue is set to false, there is no way to tell
/// if the deletion was successful (without subsequently trying to access
/// the setting just deleted). Therefore, use caution when deleting settings
/// from the settings in this manner.
/// <para>
/// The string 'settingName' is case-sensitive.
/// </para>
/// </remarks>
public void DeleteSetting(string settingName, bool throwOnMissingValue)
{
#region conditions
//is this key readonly
if (isReadOnly)
{
throwKeyReadOnlyException();
}
//is the parameter settingName null or an empty string
if (settingName == null || settingName == String.Empty)
{
throwParameterNullException("name");
}
//is setting missing and throwOnMissingValue set to true
if (!data.HasAttribute(settingName) & throwOnMissingValue)
{
throwInvalidValueRefException("name");
}
#endregion
//all conditions met and setting exsist delete setting
if (data.HasAttribute(settingName))
{
data.RemoveAttribute(settingName);
}
}
#endregion
#region GetSubKeyNames
//GetSubKeyNames
/// <summary>
/// Retrieves an array of strings that contains all the subkey names.
/// </summary>
///
/// <returns>
/// An array of strings that contains the names of
/// the subkeys for the current SettingsKey.
/// </returns>
///
/// <remarks>
/// This method does not recursively find names.
/// It returns the names on the base level it was called from.
/// </remarks>
public string[] GetSubKeyNames()
{
string[] names;
int index;
XmlElement node;
string[] outNames;
IEnumerator enumerator;
names = new String[checked((uint) this.data.ChildNodes.Count)];
index = 0;
enumerator = this.data.ChildNodes.GetEnumerator();
while (enumerator.MoveNext())
{
node = (XmlElement) enumerator.Current;
names[index] = node.Name;
index++;
}
outNames = names;
return outNames;
}
#endregion
#region GetSettingNames
//GetSettingNames
/// <summary>
/// Retrieves an array of strings that contains all
/// the setting names associated with this SettingsKey.
/// </summary>
///
/// <returns>
/// An array of strings that contains the setting names for the current key.
/// </returns>
///
/// <remarks>
/// If no setting names for the key are found, an empty array is returned.
/// </remarks>
public string[] GetSettingNames()
{
string[] names;
int index;
XmlAttribute attribute;
string[] outNames;
IEnumerator enumerator;
names = new String[checked((uint) this.data.Attributes.Count)];
index = 0;
enumerator = this.data.Attributes.GetEnumerator();
while (enumerator.MoveNext())
{
attribute = (XmlAttribute) enumerator.Current;
names[index] = attribute.Name;
index++;
}
outNames = names;
return outNames;
}
#endregion
#region GetSetting