<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Advapi32\Interop.RegistryConstants.cs">
<Link>Common\CoreLib\Interop\Windows\Advapi32\Interop.RegistryConstants.cs</Link>
</Compile>
- <Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\Registry.cs">
- <Link>Common\CoreLib\Microsoft\Win32\Registry.cs</Link>
- </Compile>
- <Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\RegistryHive.cs">
- <Link>Common\CoreLib\Microsoft\Win32\RegistryHive.cs</Link>
- </Compile>
- <Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\RegistryValueKind.cs">
- <Link>Common\CoreLib\Microsoft\Win32\RegistryValueKind.cs</Link>
- </Compile>
<Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\SafeHandles\SafeRegistryHandle.cs">
<Link>Common\CoreLib\Microsoft\Win32\SafeHandles\SafeRegistryHandle.cs</Link>
</Compile>
+ <Compile Include="Microsoft\Win32\Registry.cs" />
+ <Compile Include="Microsoft\Win32\RegistryHive.cs" />
+ <Compile Include="Microsoft\Win32\RegistryValueKind.cs" />
<Compile Include="Microsoft\Win32\RegistryKey.cs" />
<Compile Include="Microsoft\Win32\RegistryKeyPermissionCheck.cs" />
- <Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\RegistryOptions.cs">
- <Link>Common\CoreLib\Microsoft\Win32\RegistryOptions.cs</Link>
- </Compile>
- <Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\RegistryValueOptions.cs">
- <Link>Common\CoreLib\Microsoft\Win32\RegistryValueOptions.cs</Link>
- </Compile>
- <Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\RegistryView.cs">
- <Link>Common\CoreLib\Microsoft\Win32\RegistryView.cs</Link>
- </Compile>
- <Compile Include="$(CommonPath)\CoreLib\System\Security\AccessControl\RegistryRights.cs">
- <Link>Common\CoreLib\System\Security\AccessControl\RegistryRights.cs</Link>
- </Compile>
+ <Compile Include="Microsoft\Win32\RegistryOptions.cs" />
+ <Compile Include="Microsoft\Win32\RegistryValueOptions.cs" />
+ <Compile Include="Microsoft\Win32\RegistryView.cs" />
+ <Compile Include="System\Security\AccessControl\RegistryRights.cs" />
<Compile Include="System\Security\AccessControl\RegistrySecurity.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)' != 'true' AND '$(TargetsWindows)' == 'true'">
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Diagnostics;
+
+namespace Microsoft.Win32
+{
+ /// <summary>Registry encapsulation. Contains members representing all top level system keys.</summary>
+ public static class Registry
+ {
+ /// <summary>Current User Key. This key should be used as the root for all user specific settings.</summary>
+ public static readonly RegistryKey CurrentUser = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
+
+ /// <summary>Local Machine key. This key should be used as the root for all machine specific settings.</summary>
+ public static readonly RegistryKey LocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default);
+
+ /// <summary>Classes Root Key. This is the root key of class information.</summary>
+ public static readonly RegistryKey ClassesRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default);
+
+ /// <summary>Users Root Key. This is the root of users.</summary>
+ public static readonly RegistryKey Users = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Default);
+
+ /// <summary>Performance Root Key. This is where dynamic performance data is stored on NT.</summary>
+ public static readonly RegistryKey PerformanceData = RegistryKey.OpenBaseKey(RegistryHive.PerformanceData, RegistryView.Default);
+
+ /// <summary>Current Config Root Key. This is where current configuration information is stored.</summary>
+ public static readonly RegistryKey CurrentConfig = RegistryKey.OpenBaseKey(RegistryHive.CurrentConfig, RegistryView.Default);
+
+ /// <summary>
+ /// Parse a keyName and returns the basekey for it.
+ /// It will also store the subkey name in the out parameter.
+ /// If the keyName is not valid, we will throw ArgumentException.
+ /// The return value shouldn't be null.
+ /// </summary>
+ private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName)
+ {
+ if (keyName == null)
+ {
+ throw new ArgumentNullException(nameof(keyName));
+ }
+
+ int i = keyName.IndexOf('\\');
+ int length = i != -1 ? i : keyName.Length;
+
+ // Determine the potential base key from the length.
+ RegistryKey baseKey = null;
+ switch (length)
+ {
+ case 10: baseKey = Users; break; // HKEY_USERS
+ case 17: baseKey = char.ToUpperInvariant(keyName[6]) == 'L' ? ClassesRoot : CurrentUser; break; // HKEY_C[L]ASSES_ROOT, otherwise HKEY_CURRENT_USER
+ case 18: baseKey = LocalMachine; break; // HKEY_LOCAL_MACHINE
+ case 19: baseKey = CurrentConfig; break; // HKEY_CURRENT_CONFIG
+ case 21: baseKey = PerformanceData; break; // HKEY_PERFORMANCE_DATA
+ }
+
+ // If a potential base key was found, see if keyName actually starts with the potential base key's name.
+ if (baseKey != null && keyName.StartsWith(baseKey.Name, StringComparison.OrdinalIgnoreCase))
+ {
+ subKeyName = (i == -1 || i == keyName.Length) ?
+ string.Empty :
+ keyName.Substring(i + 1, keyName.Length - i - 1);
+
+ return baseKey;
+ }
+
+ throw new ArgumentException(SR.Format(SR.Arg_RegInvalidKeyName, nameof(keyName)), nameof(keyName));
+ }
+
+ public static object GetValue(string keyName, string valueName, object defaultValue)
+ {
+ string subKeyName;
+ RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
+
+ using (RegistryKey key = basekey.OpenSubKey(subKeyName))
+ {
+ return key?.GetValue(valueName, defaultValue);
+ }
+ }
+
+ public static void SetValue(string keyName, string valueName, object value)
+ {
+ SetValue(keyName, valueName, value, RegistryValueKind.Unknown);
+ }
+
+ public static void SetValue(string keyName, string valueName, object value, RegistryValueKind valueKind)
+ {
+ string subKeyName;
+ RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
+
+ using (RegistryKey key = basekey.CreateSubKey(subKeyName))
+ {
+ Debug.Assert(key != null, "An exception should be thrown if failed!");
+ key.SetValue(valueName, value, valueKind);
+ }
+ }
+ }
+}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace Microsoft.Win32
+{
+ /**
+ * Registry hive values. Useful only for GetRemoteBaseKey
+ */
+ public enum RegistryHive
+ {
+ ClassesRoot = unchecked((int)0x80000000),
+ CurrentUser = unchecked((int)0x80000001),
+ LocalMachine = unchecked((int)0x80000002),
+ Users = unchecked((int)0x80000003),
+ PerformanceData = unchecked((int)0x80000004),
+ CurrentConfig = unchecked((int)0x80000005),
+ }
+}
namespace Microsoft.Win32
{
-#if REGISTRY_ASSEMBLY
- public
-#else
- internal
-#endif
- sealed partial class RegistryKey : MarshalByRefObject, IDisposable
+ public sealed partial class RegistryKey : MarshalByRefObject, IDisposable
{
private void ClosePerfDataKey()
{
namespace Microsoft.Win32
{
-#if REGISTRY_ASSEMBLY
- public
-#else
- internal
-#endif
- sealed partial class RegistryKey : MarshalByRefObject, IDisposable
+ public sealed partial class RegistryKey : MarshalByRefObject, IDisposable
{
private void ClosePerfDataKey()
{
ret = Interop.Advapi32.RegSetValueEx(_hkey,
name,
0,
- valueKind,
+ (int)valueKind,
data,
checked(data.Length * 2 + 2));
break;
ret = Interop.Advapi32.RegSetValueEx(_hkey,
name,
0,
- RegistryValueKind.MultiString,
+ Interop.Advapi32.RegistryValues.REG_MULTI_SZ,
dataChars,
sizeInBytes);
ret = Interop.Advapi32.RegSetValueEx(_hkey,
name,
0,
- (valueKind == RegistryValueKind.None ? Interop.Advapi32.RegistryValues.REG_NONE : RegistryValueKind.Binary),
+ (valueKind == RegistryValueKind.None ? Interop.Advapi32.RegistryValues.REG_NONE : Interop.Advapi32.RegistryValues.REG_BINARY),
dataBytes,
dataBytes.Length);
break;
ret = Interop.Advapi32.RegSetValueEx(_hkey,
name,
0,
- RegistryValueKind.DWord,
+ Interop.Advapi32.RegistryValues.REG_DWORD,
ref data,
4);
break;
ret = Interop.Advapi32.RegSetValueEx(_hkey,
name,
0,
- RegistryValueKind.QWord,
+ Interop.Advapi32.RegistryValues.REG_QWORD,
ref data,
8);
break;
namespace Microsoft.Win32
{
/// <summary>Registry encapsulation. To get an instance of a RegistryKey use the Registry class's static members then call OpenSubKey.</summary>
-#if REGISTRY_ASSEMBLY
- public
-#else
- internal
-#endif
- sealed partial class RegistryKey : MarshalByRefObject, IDisposable
+ 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));
namespace Microsoft.Win32
{
-#if REGISTRY_ASSEMBLY
- public
-#else
- internal
-#endif
- enum RegistryKeyPermissionCheck
+ public enum RegistryKeyPermissionCheck
{
Default = 0,
ReadSubTree = 1,
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+
+namespace Microsoft.Win32
+{
+ [Flags]
+ public enum RegistryOptions
+ {
+ None = Interop.Advapi32.RegistryOptions.REG_OPTION_NON_VOLATILE, // 0x0000
+ Volatile = Interop.Advapi32.RegistryOptions.REG_OPTION_VOLATILE, // 0x0001
+ };
+}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace Microsoft.Win32
+{
+ public enum RegistryValueKind
+ {
+ String = Interop.Advapi32.RegistryValues.REG_SZ,
+ ExpandString = Interop.Advapi32.RegistryValues.REG_EXPAND_SZ,
+ Binary = Interop.Advapi32.RegistryValues.REG_BINARY,
+ DWord = Interop.Advapi32.RegistryValues.REG_DWORD,
+ MultiString = Interop.Advapi32.RegistryValues.REG_MULTI_SZ,
+ QWord = Interop.Advapi32.RegistryValues.REG_QWORD,
+ Unknown = 0, // REG_NONE is defined as zero but BCL
+ None = unchecked((int)0xFFFFFFFF), // mistakenly overrode this value.
+ } // Now instead of using Interop.Kernel32.RegistryValues.REG_NONE we use "-1".
+}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+
+namespace Microsoft.Win32
+{
+ [Flags]
+ public enum RegistryValueOptions
+ {
+ None = 0,
+ DoNotExpandEnvironmentNames = 1
+ }
+}
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+
+namespace Microsoft.Win32
+{
+ public enum RegistryView
+ {
+ Default = 0, // 0x0000 operate on the default registry view
+ Registry64 = Interop.Advapi32.RegistryView.KEY_WOW64_64KEY, // 0x0100 operate on the 64-bit registry view
+ Registry32 = Interop.Advapi32.RegistryView.KEY_WOW64_32KEY, // 0x0200 operate on the 32-bit registry view
+ };
+}
namespace Microsoft.Win32.SafeHandles
{
-#if REGISTRY_ASSEMBLY
- public
-#else
- internal
-#endif
- sealed partial class SafeRegistryHandle : SafeHandleZeroOrMinusOneIsInvalid
+ public sealed partial class SafeRegistryHandle : SafeHandleZeroOrMinusOneIsInvalid
{
// TODO: implement this if necessary
protected override bool ReleaseHandle() => true;
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using Microsoft.Win32;
+
+namespace System.Security.AccessControl
+{
+ // We derived this enum from the definitions of KEY_READ and such from
+ // winnt.h and from MSDN, plus some experimental validation with regedit.
+ // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_key_security_and_access_rights.asp
+ [Flags]
+ public enum RegistryRights
+ {
+ // No None field - An ACE with the value 0 cannot grant nor deny.
+ QueryValues = Interop.Advapi32.RegistryOperations.KEY_QUERY_VALUE, // 0x0001 query the values of a registry key
+ SetValue = Interop.Advapi32.RegistryOperations.KEY_SET_VALUE, // 0x0002 create, delete, or set a registry value
+ CreateSubKey = Interop.Advapi32.RegistryOperations.KEY_CREATE_SUB_KEY, // 0x0004 required to create a subkey of a specific key
+ EnumerateSubKeys = Interop.Advapi32.RegistryOperations.KEY_ENUMERATE_SUB_KEYS, // 0x0008 required to enumerate sub keys of a key
+ Notify = Interop.Advapi32.RegistryOperations.KEY_NOTIFY, // 0x0010 needed to request change notifications
+ CreateLink = Interop.Advapi32.RegistryOperations.KEY_CREATE_LINK, // 0x0020 reserved for system use
+ ///
+ /// The Windows Kernel team agrees that it was a bad design to expose the WOW64_n options as permissions.
+ /// in the .NET Framework these options are exposed via the RegistryView enum
+ ///
+ /// Reg64 = Interop.Advapi32.RegistryOptions.KEY_WOW64_64KEY, // 0x0100 operate on the 64-bit registry view
+ /// Reg32 = Interop.Advapi32.RegistryOptions.KEY_WOW64_32KEY, // 0x0200 operate on the 32-bit registry view
+ ExecuteKey = ReadKey,
+ ReadKey = Interop.Advapi32.RegistryOperations.STANDARD_RIGHTS_READ | QueryValues | EnumerateSubKeys | Notify,
+ WriteKey = Interop.Advapi32.RegistryOperations.STANDARD_RIGHTS_WRITE | SetValue | CreateSubKey,
+ Delete = 0x10000,
+ ReadPermissions = 0x20000,
+ ChangePermissions = 0x40000,
+ TakeOwnership = 0x80000,
+ FullControl = 0xF003F | Interop.Advapi32.RegistryOperations.STANDARD_RIGHTS_READ | Interop.Advapi32.RegistryOperations.STANDARD_RIGHTS_WRITE
+ }
+}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>{20A2BA2C-5517-483F-8FFE-643441A59852}</ProjectGuid>
+ <DefineConstants>$(DefineConstants);REGISTRY_ASSEMBLY</DefineConstants>
<Configurations>netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release</Configurations>
</PropertyGroup>
<ItemGroup>
SafeRegistryHandle handle = TestRegistryKey.Handle;
int ret = Interop.Advapi32.RegSetValueEx(handle, TestValueName, 0,
- RegistryValueKind.MultiString, corrupt, corrupt.Length * 2);
+ (int)RegistryValueKind.MultiString, corrupt, corrupt.Length * 2);
Assert.Equal(0, ret);
try
{
SafeRegistryHandle handle = TestRegistryKey.Handle;
int ret = Interop.Advapi32.RegSetValueEx(handle, TestValueName, 0,
- kind, contents, contents.Length);
+ (int)kind, contents, contents.Length);
Assert.Equal(0, ret);
try
{