Repository navigation
Expand file tree
/
Copy pathRegistryKey.cs
More file actions
802 lines (693 loc) · 28.3 KB
/
Copy pathRegistryKey.cs
File metadata and controls
802 lines (693 loc) · 28.3 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security;
using System.Security.AccessControl;
using System.Text;
namespace Microsoft.Win32
{
/// <summary>Registry encapsulation. To get an instance of a RegistryKey use the Registry class's static members then call OpenSubKey.</summary>
public sealed partial class RegistryKey : MarshalByRefObject, IDisposable
{
private static readonly IntPtr HKEY_CLASSES_ROOT = new IntPtr(unchecked((int)0x80000000));
private static readonly IntPtr HKEY_CURRENT_USER = new IntPtr(unchecked((int)0x80000001));
private static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002));
private static readonly IntPtr HKEY_USERS = new IntPtr(unchecked((int)0x80000003));
private static readonly IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(unchecked((int)0x80000004));
private static readonly IntPtr HKEY_CURRENT_CONFIG = new IntPtr(unchecked((int)0x80000005));
/// <summary>Names of keys. This array must be in the same order as the HKEY values listed above.</summary>
private static readonly string[] s_hkeyNames = new string[]
{
"HKEY_CLASSES_ROOT",
"HKEY_CURRENT_USER",
"HKEY_LOCAL_MACHINE",
"HKEY_USERS",
"HKEY_PERFORMANCE_DATA",
"HKEY_CURRENT_CONFIG"
};
// MSDN defines the following limits for registry key names & values:
// Key Name: 255 characters
// Value name: 16,383 Unicode characters
// Value: either 1 MB or current available memory, depending on registry format.
private const int MaxKeyLength = 255;
private const int MaxValueLength = 16383;
private volatile SafeRegistryHandle _hkey;
private volatile string _keyName;
private readonly bool _remoteKey;
private volatile StateFlags _state;
private volatile RegistryKeyPermissionCheck _checkMode;
private readonly RegistryView _regView = RegistryView.Default;
/// <summary>
/// Creates a RegistryKey. This key is bound to hkey, if writable is <b>false</b> then no write operations will be allowed.
/// </summary>
private RegistryKey(SafeRegistryHandle hkey, bool writable, RegistryView view) :
this(hkey, writable, false, false, false, view)
{
}
/// <summary>
/// Creates a RegistryKey.
/// This key is bound to hkey, if writable is <b>false</b> then no write operations
/// will be allowed. If systemkey is set then the hkey won't be released
/// when the object is GC'ed.
/// The remoteKey flag when set to true indicates that we are dealing with registry entries
/// on a remote machine and requires the program making these calls to have full trust.
/// </summary>
private RegistryKey(SafeRegistryHandle hkey, bool writable, bool systemkey, bool remoteKey, bool isPerfData, RegistryView view)
{
ValidateKeyView(view);
_hkey = hkey;
_keyName = "";
_remoteKey = remoteKey;
_regView = view;
if (systemkey)
{
_state |= StateFlags.SystemKey;
}
if (writable)
{
_state |= StateFlags.WriteAccess;
}
if (isPerfData)
{
_state |= StateFlags.PerfData;
}
}
public void Flush()
{
FlushCore();
}
public void Close()
{
Dispose();
}
public void Dispose()
{
if (_hkey != null)
{
if (!IsSystemKey())
{
try
{
_hkey.Dispose();
}
catch (IOException)
{
// we don't really care if the handle is invalid at this point
}
finally
{
_hkey = null!;
}
}
else if (IsPerfDataKey())
{
ClosePerfDataKey();
}
}
}
/// <summary>Creates a new subkey, or opens an existing one.</summary>
/// <param name="subkey">Name or path to subkey to create or open.</param>
/// <returns>The subkey, or <b>null</b> if the operation failed.</returns>
public RegistryKey CreateSubKey(string subkey)
{
return CreateSubKey(subkey, _checkMode);
}
public RegistryKey CreateSubKey(string subkey, bool writable)
{
return CreateSubKey(subkey, writable ? RegistryKeyPermissionCheck.ReadWriteSubTree : RegistryKeyPermissionCheck.ReadSubTree, RegistryOptions.None);
}
public RegistryKey CreateSubKey(string subkey, bool writable, RegistryOptions options)
{
return CreateSubKey(subkey, writable ? RegistryKeyPermissionCheck.ReadWriteSubTree : RegistryKeyPermissionCheck.ReadSubTree, options);
}
public RegistryKey CreateSubKey(string subkey, RegistryKeyPermissionCheck permissionCheck)
{
return CreateSubKey(subkey, permissionCheck, RegistryOptions.None);
}
public RegistryKey CreateSubKey(string subkey, RegistryKeyPermissionCheck permissionCheck, RegistryOptions registryOptions, RegistrySecurity? registrySecurity)
{
return CreateSubKey(subkey, permissionCheck, registryOptions);
}
public RegistryKey CreateSubKey(string subkey, RegistryKeyPermissionCheck permissionCheck, RegistrySecurity? registrySecurity)
{
return CreateSubKey(subkey, permissionCheck, RegistryOptions.None);
}
public RegistryKey CreateSubKey(string subkey, RegistryKeyPermissionCheck permissionCheck, RegistryOptions registryOptions)
{
ValidateKeyOptions(registryOptions);
ValidateKeyName(subkey);
ValidateKeyMode(permissionCheck);
EnsureWriteable();
subkey = FixupName(subkey); // Fixup multiple slashes to a single slash
// only keys opened under read mode is not writable
if (!_remoteKey)
{
RegistryKey? key = InternalOpenSubKeyWithoutSecurityChecks(subkey, (permissionCheck != RegistryKeyPermissionCheck.ReadSubTree));
if (key != null)
{
// Key already exits
key._checkMode = permissionCheck;
return key;
}
}
return CreateSubKeyInternalCore(subkey, permissionCheck, registryOptions);
}
/// <summary>
/// Deletes the specified subkey. Will throw an exception if the subkey has
/// subkeys. To delete a tree of subkeys use, DeleteSubKeyTree.
/// </summary>
/// <param name="subkey">SubKey to delete.</param>
/// <exception cref="InvalidOperationException">Thrown if the subkey has child subkeys.</exception>
public void DeleteSubKey(string subkey)
{
DeleteSubKey(subkey, true);
}
public void DeleteSubKey(string subkey, bool throwOnMissingSubKey)
{
ValidateKeyName(subkey);
EnsureWriteable();
subkey = FixupName(subkey); // Fixup multiple slashes to a single slash
// Open the key we are deleting and check for children. Be sure to
// explicitly call close to avoid keeping an extra HKEY open.
//
RegistryKey? key = InternalOpenSubKeyWithoutSecurityChecks(subkey, false);
if (key != null)
{
using (key)
{
if (key.SubKeyCount > 0)
{
throw new InvalidOperationException(SR.InvalidOperation_RegRemoveSubKey);
}
}
DeleteSubKeyCore(subkey, throwOnMissingSubKey);
}
else // there is no key which also means there is no subkey
{
if (throwOnMissingSubKey)
{
throw new ArgumentException(SR.Arg_RegSubKeyAbsent);
}
}
}
/// <summary>Recursively deletes a subkey and any child subkeys.</summary>
/// <param name="subkey">SubKey to delete.</param>
public void DeleteSubKeyTree(string subkey)
{
DeleteSubKeyTree(subkey, throwOnMissingSubKey: true);
}
public void DeleteSubKeyTree(string subkey, bool throwOnMissingSubKey)
{
ValidateKeyName(subkey);
// Security concern: Deleting a hive's "" subkey would delete all
// of that hive's contents. Don't allow "".
if (subkey.Length == 0 && IsSystemKey())
{
throw new ArgumentException(SR.Arg_RegKeyDelHive);
}
EnsureWriteable();
subkey = FixupName(subkey); // Fixup multiple slashes to a single slash
RegistryKey? key = InternalOpenSubKeyWithoutSecurityChecks(subkey, true);
if (key != null)
{
using (key)
{
if (key.SubKeyCount > 0)
{
string[] keys = key.GetSubKeyNames();
for (int i = 0; i < keys.Length; i++)
{
key.DeleteSubKeyTreeInternal(keys[i]);
}
}
}
DeleteSubKeyTreeCore(subkey);
}
else if (throwOnMissingSubKey)
{
throw new ArgumentException(SR.Arg_RegSubKeyAbsent);
}
}
/// <summary>
/// An internal version which does no security checks or argument checking. Skipping the
/// security checks should give us a slight perf gain on large trees.
/// </summary>
private void DeleteSubKeyTreeInternal(string subkey)
{
RegistryKey? key = InternalOpenSubKeyWithoutSecurityChecks(subkey, true);
if (key != null)
{
using (key)
{
if (key.SubKeyCount > 0)
{
string[] keys = key.GetSubKeyNames();
for (int i = 0; i < keys.Length; i++)
{
key.DeleteSubKeyTreeInternal(keys[i]);
}
}
}
DeleteSubKeyTreeCore(subkey);
}
else
{
throw new ArgumentException(SR.Arg_RegSubKeyAbsent);
}
}
/// <summary>Deletes the specified value from this key.</summary>
/// <param name="name">Name of value to delete.</param>
public void DeleteValue(string name)
{
DeleteValue(name, true);
}
public void DeleteValue(string name, bool throwOnMissingValue)
{
EnsureWriteable();
DeleteValueCore(name, throwOnMissingValue);
}
public static RegistryKey OpenBaseKey(RegistryHive hKey, RegistryView view)
{
ValidateKeyView(view);
return OpenBaseKeyCore(hKey, view);
}
/// <summary>Retrieves a new RegistryKey that represents the requested key on a foreign machine.</summary>
/// <param name="hKey">hKey HKEY_* to open.</param>
/// <param name="machineName">Name the machine to connect to.</param>
/// <returns>The RegistryKey requested.</returns>
public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, string machineName)
{
return OpenRemoteBaseKey(hKey, machineName, RegistryView.Default);
}
public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, string machineName, RegistryView view)
{
ArgumentNullException.ThrowIfNull(machineName);
ValidateKeyView(view);
return OpenRemoteBaseKeyCore(hKey, machineName, view);
}
/// <summary>Returns a subkey with read only permissions.</summary>
/// <param name="name">Name or path of subkey to open.</param>
/// <returns>The Subkey requested, or <b>null</b> if the operation failed.</returns>
public RegistryKey? OpenSubKey(string name)
{
return OpenSubKey(name, false);
}
/// <summary>
/// Retrieves a subkey. If readonly is <b>true</b>, then the subkey is opened with
/// read-only access.
/// </summary>
/// <param name="name">Name or the path of subkey to open.</param>
/// <param name="writable">Set to <b>true</b> if you only need readonly access.</param>
/// <returns>the Subkey requested, or <b>null</b> if the operation failed.</returns>
public RegistryKey? OpenSubKey(string name, bool writable)
{
ValidateKeyName(name);
EnsureNotDisposed();
name = FixupName(name);
return InternalOpenSubKeyCore(name, writable);
}
public RegistryKey? OpenSubKey(string name, RegistryKeyPermissionCheck permissionCheck)
{
ValidateKeyMode(permissionCheck);
return OpenSubKey(name, permissionCheck, (RegistryRights)GetRegistryKeyAccess(permissionCheck));
}
public RegistryKey? OpenSubKey(string name, RegistryRights rights)
{
return OpenSubKey(name, this._checkMode, rights);
}
public RegistryKey? OpenSubKey(string name, RegistryKeyPermissionCheck permissionCheck, RegistryRights rights)
{
ValidateKeyName(name);
ValidateKeyMode(permissionCheck);
ValidateKeyRights(rights);
EnsureNotDisposed();
name = FixupName(name); // Fixup multiple slashes to a single slash
return InternalOpenSubKeyCore(name, permissionCheck, (int)rights);
}
internal RegistryKey? InternalOpenSubKeyWithoutSecurityChecks(string name, bool writable)
{
ValidateKeyName(name);
EnsureNotDisposed();
return InternalOpenSubKeyWithoutSecurityChecksCore(name, writable);
}
public RegistrySecurity GetAccessControl()
{
return GetAccessControl(AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group);
}
public RegistrySecurity GetAccessControl(AccessControlSections includeSections)
{
EnsureNotDisposed();
return new RegistrySecurity(Handle, Name, includeSections);
}
public void SetAccessControl(RegistrySecurity registrySecurity)
{
EnsureWriteable();
ArgumentNullException.ThrowIfNull(registrySecurity);
registrySecurity.Persist(Handle, Name);
}
/// <summary>Retrieves the count of subkeys.</summary>
/// <returns>A count of subkeys.</returns>
public int SubKeyCount
{
get
{
EnsureNotDisposed();
return InternalSubKeyCountCore();
}
}
public RegistryView View
{
get
{
EnsureNotDisposed();
return _regView;
}
}
public SafeRegistryHandle Handle
{
get
{
EnsureNotDisposed();
return IsSystemKey() ? SystemKeyHandle : _hkey;
}
}
public static RegistryKey FromHandle(SafeRegistryHandle handle)
{
return FromHandle(handle, RegistryView.Default);
}
public static RegistryKey FromHandle(SafeRegistryHandle handle, RegistryView view)
{
ArgumentNullException.ThrowIfNull(handle);
ValidateKeyView(view);
return new RegistryKey(handle, writable: true, view: view);
}
/// <summary>Retrieves an array of strings containing all the subkey names.</summary>
/// <returns>All subkey names.</returns>
public string[] GetSubKeyNames()
{
EnsureNotDisposed();
int subkeys = SubKeyCount;
return subkeys > 0 ?
InternalGetSubKeyNamesCore(subkeys) :
Array.Empty<string>();
}
/// <summary>Retrieves the count of values.</summary>
/// <returns>A count of values.</returns>
public int ValueCount
{
get
{
EnsureNotDisposed();
return InternalValueCountCore();
}
}
/// <summary>Retrieves an array of strings containing all the value names.</summary>
/// <returns>All value names.</returns>
public string[] GetValueNames()
{
EnsureNotDisposed();
int values = ValueCount;
return values > 0 ?
GetValueNamesCore(values) :
Array.Empty<string>();
}
/// <summary>Retrieves the specified value. <b>null</b> is returned if the value doesn't exist</summary>
/// <remarks>
/// Note that <var>name</var> can be null or "", at which point the
/// unnamed or default value of this Registry key is returned, if any.
/// </remarks>
/// <param name="name">Name of value to retrieve.</param>
/// <returns>The data associated with the value.</returns>
public object? GetValue(string? name)
{
return InternalGetValue(name, null, false);
}
/// <summary>Retrieves the specified value. <i>defaultValue</i> is returned if the value doesn't exist.</summary>
/// <remarks>
/// Note that <var>name</var> can be null or "", at which point the
/// unnamed or default value of this Registry key is returned, if any.
/// The default values for RegistryKeys are OS-dependent. NT doesn't
/// have them by default, but they can exist and be of any type. On
/// Win95, the default value is always an empty key of type REG_SZ.
/// Win98 supports default values of any type, but defaults to REG_SZ.
/// </remarks>
/// <param name="name">Name of value to retrieve.</param>
/// <param name="defaultValue">Value to return if <i>name</i> doesn't exist.</param>
/// <returns>The data associated with the value.</returns>
[return: NotNullIfNotNull(nameof(defaultValue))]
public object? GetValue(string? name, object? defaultValue)
{
return InternalGetValue(name, defaultValue, false);
}
[return: NotNullIfNotNull(nameof(defaultValue))]
public object? GetValue(string? name, object? defaultValue, RegistryValueOptions options)
{
if (options < RegistryValueOptions.None || options > RegistryValueOptions.DoNotExpandEnvironmentNames)
{
throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options));
}
bool doNotExpand = (options == RegistryValueOptions.DoNotExpandEnvironmentNames);
return InternalGetValue(name, defaultValue, doNotExpand);
}
[return: NotNullIfNotNull(nameof(defaultValue))]
private object? InternalGetValue(string? name, object? defaultValue, bool doNotExpand)
{
EnsureNotDisposed();
return InternalGetValueCore(name, defaultValue, doNotExpand);
}
public RegistryValueKind GetValueKind(string? name)
{
EnsureNotDisposed();
return GetValueKindCore(name);
}
public string Name
{
get
{
EnsureNotDisposed();
return _keyName;
}
}
/// <summary>Sets the specified value.</summary>
/// <param name="name">Name of value to store data in.</param>
/// <param name="value">Data to store.</param>
public void SetValue(string? name, object value)
{
SetValue(name, value, RegistryValueKind.Unknown);
}
public void SetValue(string? name, object value, RegistryValueKind valueKind)
{
ArgumentNullException.ThrowIfNull(value);
if (name != null && name.Length > MaxValueLength)
{
throw new ArgumentException(SR.Arg_RegValStrLenBug, nameof(name));
}
if (!Enum.IsDefined(typeof(RegistryValueKind), valueKind))
{
throw new ArgumentException(SR.Arg_RegBadKeyKind, nameof(valueKind));
}
EnsureWriteable();
if (valueKind == RegistryValueKind.Unknown)
{
// this is to maintain compatibility with the old way of autodetecting the type.
// SetValue(string, object) will come through this codepath.
valueKind = CalculateValueKind(value);
}
SetValueCore(name, value, valueKind);
}
private static RegistryValueKind CalculateValueKind(object value)
{
// This logic matches what used to be in SetValue(string name, object value) in the v1.0 and v1.1 days.
// Even though we could add detection for an int64 in here, we want to maintain compatibility with the
// old behavior.
if (value is int)
{
return RegistryValueKind.DWord;
}
else if (value is Array)
{
if (value is byte[])
{
return RegistryValueKind.Binary;
}
else if (value is string[])
{
return RegistryValueKind.MultiString;
}
else
{
throw new ArgumentException(SR.Format(SR.Arg_RegSetBadArrType, value.GetType().Name));
}
}
else
{
return RegistryValueKind.String;
}
}
/// <summary>Retrieves a string representation of this key.</summary>
/// <returns>A string representing the key.</returns>
public override string ToString()
{
EnsureNotDisposed();
return _keyName;
}
private static string FixupName(string name)
{
Debug.Assert(name != null, "[FixupName]name!=null");
if (!name.Contains('\\'))
{
return name;
}
StringBuilder sb = new StringBuilder(name);
FixupPath(sb);
int temp = sb.Length - 1;
if (temp >= 0 && sb[temp] == '\\') // Remove trailing slash
{
sb.Length = temp;
}
return sb.ToString();
}
private static void FixupPath(StringBuilder path)
{
Debug.Assert(path != null);
int length = path.Length;
bool fixup = false;
char markerChar = (char)0xFFFF;
int i = 1;
while (i < length - 1)
{
if (path[i] == '\\')
{
i++;
while (i < length && path[i] == '\\')
{
path[i] = markerChar;
i++;
fixup = true;
}
}
i++;
}
if (fixup)
{
i = 0;
int j = 0;
while (i < length)
{
if (path[i] == markerChar)
{
i++;
continue;
}
path[j] = path[i];
i++;
j++;
}
path.Length += j - i;
}
}
private void EnsureNotDisposed()
{
if (_hkey == null)
{
throw new ObjectDisposedException(_keyName, SR.ObjectDisposed_RegKeyClosed);
}
}
private void EnsureWriteable()
{
EnsureNotDisposed();
if (!IsWritable())
{
throw new UnauthorizedAccessException(SR.UnauthorizedAccess_RegistryNoWrite);
}
}
private RegistryKeyPermissionCheck GetSubKeyPermissionCheck(bool subkeyWritable)
{
if (_checkMode == RegistryKeyPermissionCheck.Default)
{
return _checkMode;
}
if (subkeyWritable)
{
return RegistryKeyPermissionCheck.ReadWriteSubTree;
}
else
{
return RegistryKeyPermissionCheck.ReadSubTree;
}
}
private static void ValidateKeyName(string name)
{
ArgumentNullException.ThrowIfNull(name);
int nextSlash = name.IndexOf('\\');
int current = 0;
while (nextSlash >= 0)
{
if ((nextSlash - current) > MaxKeyLength)
{
throw new ArgumentException(SR.Arg_RegKeyStrLenBug, nameof(name));
}
current = nextSlash + 1;
nextSlash = name.IndexOf('\\', current);
}
if ((name.Length - current) > MaxKeyLength)
{
throw new ArgumentException(SR.Arg_RegKeyStrLenBug, nameof(name));
}
}
private static void ValidateKeyMode(RegistryKeyPermissionCheck mode)
{
if (mode < RegistryKeyPermissionCheck.Default || mode > RegistryKeyPermissionCheck.ReadWriteSubTree)
{
throw new ArgumentException(SR.Argument_InvalidRegistryKeyPermissionCheck, nameof(mode));
}
}
private static void ValidateKeyOptions(RegistryOptions options)
{
if (options < RegistryOptions.None || options > RegistryOptions.Volatile)
{
throw new ArgumentException(SR.Argument_InvalidRegistryOptionsCheck, nameof(options));
}
}
private static void ValidateKeyView(RegistryView view)
{
if (view != RegistryView.Default && view != RegistryView.Registry32 && view != RegistryView.Registry64)
{
throw new ArgumentException(SR.Argument_InvalidRegistryViewCheck, nameof(view));
}
}
private static void ValidateKeyRights(RegistryRights rights)
{
if (0 != (rights & ~RegistryRights.FullControl))
{
// We need to throw SecurityException here for compatibility reason,
// although UnauthorizedAccessException will make more sense.
throw new SecurityException(SR.Security_RegistryPermission);
}
}
/// <summary>Retrieves the current state of the dirty property.</summary>
/// <remarks>A key is marked as dirty if any operation has occurred that modifies the contents of the key.</remarks>
/// <returns><b>true</b> if the key has been modified.</returns>
private bool IsDirty() => (_state & StateFlags.Dirty) != 0;
private bool IsSystemKey() => (_state & StateFlags.SystemKey) != 0;
private bool IsWritable() => (_state & StateFlags.WriteAccess) != 0;
private bool IsPerfDataKey() => (_state & StateFlags.PerfData) != 0;
private void SetDirty() => _state |= StateFlags.Dirty;
[Flags]
private enum StateFlags
{
/// <summary>Dirty indicates that we have munged data that should be potentially written to disk.</summary>
Dirty = 0x0001,
/// <summary>SystemKey indicates that this is a "SYSTEMKEY" and shouldn't be "opened" or "closed".</summary>
SystemKey = 0x0002,
/// <summary>Access</summary>
WriteAccess = 0x0004,
/// <summary>Indicates if this key is for HKEY_PERFORMANCE_DATA</summary>
PerfData = 0x0008
}
}
}