Moved registry.cs to shared (#19471)
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Thu, 16 Aug 2018 20:21:55 +0000 (13:21 -0700)
committerGitHub <noreply@github.com>
Thu, 16 Aug 2018 20:21:55 +0000 (13:21 -0700)
* moved registry to shared

* using corefx version

* set value added

src/System.Private.CoreLib/System.Private.CoreLib.csproj
src/System.Private.CoreLib/shared/Microsoft/Win32/Registry.cs [new file with mode: 0644]
src/System.Private.CoreLib/shared/Microsoft/Win32/RegistryHive.cs [new file with mode: 0644]
src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems
src/System.Private.CoreLib/shared/System/Globalization/HijriCalendar.Win32.cs
src/System.Private.CoreLib/shared/System/Globalization/JapaneseCalendar.Win32.cs
src/System.Private.CoreLib/src/Microsoft/Win32/Registry.cs [deleted file]
src/System.Private.CoreLib/src/Microsoft/Win32/RegistryKey.cs

index 70c5749..8e8fe3e 100644 (file)
   <ItemGroup>
     <Compile Include="$(BclSourcesRoot)\Microsoft\Win32\UnsafeNativeMethods.cs" />
     <Compile Include="$(BclSourcesRoot)\Microsoft\Win32\Win32Native.cs" />
-    <Compile Condition="'$(FeatureWin32Registry)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\Registry.cs" />
     <Compile Condition="'$(FeatureWin32Registry)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\RegistryKey.cs" />
     <Compile Condition="'$(FeatureClassicCominterop)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\OAVariantLib.cs" />
   </ItemGroup>
diff --git a/src/System.Private.CoreLib/shared/Microsoft/Win32/Registry.cs b/src/System.Private.CoreLib/shared/Microsoft/Win32/Registry.cs
new file mode 100644 (file)
index 0000000..bc4ee08
--- /dev/null
@@ -0,0 +1,104 @@
+// 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>
+#if REGISTRY_ASSEMBLY
+    public
+#else
+    internal
+#endif
+    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);
+            }
+        }
+    }
+}
diff --git a/src/System.Private.CoreLib/shared/Microsoft/Win32/RegistryHive.cs b/src/System.Private.CoreLib/shared/Microsoft/Win32/RegistryHive.cs
new file mode 100644 (file)
index 0000000..0f1e954
--- /dev/null
@@ -0,0 +1,24 @@
+// 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
+     */
+#if REGISTRY_ASSEMBLY
+    public
+#else
+    internal
+#endif
+    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),
+    }
+}
\ No newline at end of file
index 6d0952e..ef48dc6 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\NtDll\NtQueryInformationFile.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Win32.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\TimeZoneInfo.Win32.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\Registry.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\RegistryHive.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeRegistryHandle.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeRegistryHandle.Windows.cs" />
index 42a79ec..7333f87 100644 (file)
@@ -45,7 +45,7 @@ namespace System.Globalization
             try
             {
                 // Open in read-only mode.
-                key = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER).OpenSubKey(InternationalRegKey, false);
+                key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser).OpenSubKey(InternationalRegKey, false);
             }
             //If this fails for any reason, we'll just return 0.
             catch (ObjectDisposedException) { return 0; }
index f4787a6..2356afe 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Globalization
             try
             {
                 // Need to access registry
-                RegistryKey key = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE).OpenSubKey(c_japaneseErasHive, false);
+                RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine).OpenSubKey(c_japaneseErasHive, false);
 
                 // Abort if we didn't find anything
                 if (key == null) return null;
diff --git a/src/System.Private.CoreLib/src/Microsoft/Win32/Registry.cs b/src/System.Private.CoreLib/src/Microsoft/Win32/Registry.cs
deleted file mode 100644 (file)
index 11f9590..0000000
+++ /dev/null
@@ -1,145 +0,0 @@
-// 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;
-using System.Runtime.InteropServices;
-using System.Runtime.Versioning;
-
-namespace Microsoft.Win32
-{
-    /**
-     * Registry encapsulation. Contains members representing all top level system
-     * keys.
-     *
-     * @security(checkClassLinking=on)
-     */
-    //This class contains only static members and does not need to be serializable.
-    internal static class Registry
-    {
-        /**
-         * Current User Key.
-         * 
-         * This key should be used as the root for all user specific settings.
-         */
-        public static readonly RegistryKey CurrentUser = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER);
-
-        /**
-         * Local Machine Key.
-         * 
-         * This key should be used as the root for all machine specific settings.
-         */
-        public static readonly RegistryKey LocalMachine = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE);
-
-        /**
-         * Classes Root Key.
-         * 
-         * This is the root key of class information.
-         */
-        public static readonly RegistryKey ClassesRoot = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT);
-
-        /**
-         * Users Root Key.
-         * 
-         * This is the root of users.
-         */
-        public static readonly RegistryKey Users = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS);
-
-        /**
-         * Performance Root Key.
-         * 
-         * This is where dynamic performance data is stored on NT.
-         */
-        public static readonly RegistryKey PerformanceData = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
-
-        /**
-         * Current Config Root Key.
-         * 
-         * This is where current configuration information is stored.
-         */
-        public static readonly RegistryKey CurrentConfig = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG);
-
-        //
-        // Following function will 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. 
-        //
-        private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName)
-        {
-            if (keyName == null)
-            {
-                throw new ArgumentNullException(nameof(keyName));
-            }
-
-            string basekeyName;
-            int i = keyName.IndexOf('\\');
-            if (i != -1)
-            {
-                basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
-            }
-            else
-            {
-                basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
-            }
-            RegistryKey basekey = null;
-
-            switch (basekeyName)
-            {
-                case "HKEY_CURRENT_USER":
-                    basekey = Registry.CurrentUser;
-                    break;
-                case "HKEY_LOCAL_MACHINE":
-                    basekey = Registry.LocalMachine;
-                    break;
-                case "HKEY_CLASSES_ROOT":
-                    basekey = Registry.ClassesRoot;
-                    break;
-                case "HKEY_USERS":
-                    basekey = Registry.Users;
-                    break;
-                case "HKEY_PERFORMANCE_DATA":
-                    basekey = Registry.PerformanceData;
-                    break;
-                case "HKEY_CURRENT_CONFIG":
-                    basekey = Registry.CurrentConfig;
-                    break;
-                default:
-                    throw new ArgumentException(SR.Format(SR.Arg_RegInvalidKeyName, nameof(keyName)));
-            }
-            if (i == -1 || i == keyName.Length)
-            {
-                subKeyName = string.Empty;
-            }
-            else
-            {
-                subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
-            }
-            return basekey;
-        }
-
-        public static object GetValue(string keyName, string valueName, object defaultValue)
-        {
-            string subKeyName;
-            RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
-            Debug.Assert(basekey != null, "basekey can't be null.");
-            RegistryKey key = basekey.OpenSubKey(subKeyName);
-            if (key == null)
-            { // if the key doesn't exist, do nothing
-                return null;
-            }
-            try
-            {
-                return key.GetValue(valueName, defaultValue);
-            }
-            finally
-            {
-                key.Close();
-            }
-        }
-    }
-}
-
-
index 69ac1cd..25aa39b 100644 (file)
@@ -135,6 +135,22 @@ namespace Microsoft.Win32
             return _keyName;
         }
 
+        public string Name
+        {
+            get
+            {
+                EnsureNotDisposed();
+                return _keyName;
+            }
+        }
+
+        // This dummy method is added to have the same implemenatation of Registry class.
+        // Its not being used anywhere.
+        public RegistryKey CreateSubKey(string subkey)
+        {
+            return null;
+        }
+
         private static void FixupPath(StringBuilder path)
         {
             Debug.Assert(path != null);
@@ -297,13 +313,14 @@ namespace Microsoft.Win32
          *
          * @return the RegistryKey requested.
          */
-        internal static RegistryKey GetBaseKey(IntPtr hKey)
+        internal static RegistryKey OpenBaseKey(RegistryHive hKey)
         {
-            return GetBaseKey(hKey, RegistryView.Default);
+            return OpenBaseKey(hKey, RegistryView.Default);
         }
 
-        internal static RegistryKey GetBaseKey(IntPtr hKey, RegistryView view)
+        internal static RegistryKey OpenBaseKey(RegistryHive hKeyHive, RegistryView view)
         {
+            IntPtr hKey = (IntPtr)((int)hKeyHive);
             int index = ((int)hKey) & 0x0FFFFFFF;
             Debug.Assert(index >= 0 && index < s_hkeyNames.Length, "index is out of range!");
             Debug.Assert((((int)hKey) & 0xFFFFFFF0) == 0x80000000, "Invalid hkey value!");