⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registry.cs

📁 在windows mobile 5上的地图查看器
💻 CS
字号:
using System;
using Win32 = Microsoft.WindowsMobile.Utilities.Win32Utilities.SafeNativeMethods;


namespace PocketEarth
{
    /// <summary>
    /// Internal class to work with until .netcf has Registry class
    /// </summary>
    public sealed class Registry
    {
        public const string HKEY_CLASSES_ROOT = "HKEY_CLASSES_ROOT";
        public const string HKEY_CURRENT_USER = "HKEY_CURRENT_USER";
        public const string HKEY_LOCAL_MACHINE = "HKEY_LOCAL_MACHINE";
        public const string HKEY_USERS = "HKEY_USERS";

        public class RegistryException: Exception
        {
            public RegistryException():base() {}
            public RegistryException(string message):base(message) {}
            public RegistryException(string message, Exception inner):base(message, inner) {}
        }


        public enum HKey
        { 
 
            CLASSES_ROOT    =  unchecked((int) 0x80000000),
            CURRENT_USER    =  unchecked((int) 0x80000001),
            LOCAL_MACHINE   =  unchecked((int) 0x80000002),
            USERS           =  unchecked((int) 0x80000003)
        }

        public enum ErrorCodes: int
        {
            ERROR_SUCCESS = 0,
            ERROR_MORE_DATA = 234
        }

        public enum CreateKeyOptions: int
        {
            REG_OPTION_NON_VOLATILE = 0,
            REG_OPTION_VOLATILE = 1
        }

        public static IntPtr GetRootKey(HKey key)
        {
            return new IntPtr((int)key);
        }

        public enum ValueType: int
        {
            REG_STRING = 1,
            REG_BINARY = 3,
            REG_DWORD = 4,
            REG_DWORD_LITTLE_ENDIAN = 4,
            REG_DWORD_BIG_ENDIAN = 5,
            REG_MULTI_SZ = 7
        }

        public static void DeleteKey(IntPtr rootKey, string subKey)
        {
            int result = Win32.RegDeleteKey(rootKey, subKey);
            if (result != 0)
            {
                throw new RegistryException("Error Deleting Key");
            }
        }

        public static void CloseKey(IntPtr key)
        {
            int result = Win32.RegCloseKey(key);
            if (result != 0)
            {
                throw new RegistryException("Error Closing Key");
            }
        }
   
        public static void SetValue(IntPtr key, string valueName, string stringValue)
        {
            byte[] bytes;

            if (stringValue.Length == 0)
            {
                bytes = new byte[2];
                bytes[0] = 0;
                bytes[1] = 0;
            }
            else
            {
                bytes = new byte[System.Text.UnicodeEncoding.Unicode.GetByteCount(stringValue) + 2];
                System.Text.UnicodeEncoding.Unicode.GetBytes(stringValue, 0, stringValue.Length, bytes, 0);
                bytes[bytes.Length - 2] = 0;
                bytes[bytes.Length - 1] = 0;
            }

            SetValue(key, valueName, ValueType.REG_STRING, bytes);
        }

        public static void SetValue(IntPtr key, string valueName, int dwordValue)
        {
            byte[] bytes = System.BitConverter.GetBytes((int)dwordValue);
                        
            SetValue(key, valueName, ValueType.REG_DWORD, bytes);
        }

        public static void SetValue(IntPtr key, string valueName, byte[] bytes)
        {
            SetValue(key, valueName, ValueType.REG_BINARY, bytes);
        }

        public static void SetValue(IntPtr key, string valueName, ValueType type, byte[] data)
        {
            int result = Win32.RegSetValueEx(key, valueName, 0, (int)type, data, data.Length);

            if (result != 0)
            {
                throw new RegistryException("Error Creating Key");
            }
        }
        
        public static IntPtr OpenKey(IntPtr rootKey, string subkey)
        {
            IntPtr newKey = new IntPtr(0);
            
            // Create a default non-volatile key under the specified rootkey
            int result = Win32.RegOpenKeyEx(rootKey, subkey, 0, 0, ref newKey);

            if (result != 0)
            {
                newKey = IntPtr.Zero;
            }

            return newKey;
        }

        public static IntPtr CreateKey(IntPtr rootKey, string subkey, bool isVolatile)
        {

            IntPtr newKey = new IntPtr(0);
            IntPtr disposition = new IntPtr(0);
            int options;
            if (isVolatile)
            {
                options = (int)CreateKeyOptions.REG_OPTION_VOLATILE;
            }
            else
            {
                options = (int)CreateKeyOptions.REG_OPTION_NON_VOLATILE;
            }

            // Create a default non-volatile key under the specified rootkey
            int result = Win32.RegCreateKeyEx(rootKey, subkey, 0, null, options, 
                0, new IntPtr(0), ref newKey, ref disposition);

            if (result != 0)
            {
                newKey = IntPtr.Zero;
            }

            return newKey;
        }

        public static IntPtr CreateKey(IntPtr rootKey, string subkey)
        {
            return CreateKey(rootKey, subkey, false);
        }

        public static object GetValue(IntPtr rootKey, string subKey, string valueName)
        {
            IntPtr key = Registry.OpenKey(rootKey, subKey);
            object result = GetValue(key, valueName);
            Registry.CloseKey(key);

            return result;
        }

        public static object GetValue(IntPtr key, string valueName)
        {
            object result = null;
            int size = 0;
            Type type;
            int typeValue = 0;
            try
            {
                GetValueType(key, valueName, out type, out size);
                byte[] data = new byte[size];

                int hr = Win32.RegQueryValueEx(key, valueName, 0, ref typeValue, data, ref size);
                if (hr == (int)ErrorCodes.ERROR_SUCCESS)
                {
                    if (type == typeof(int))
                    {
                        result = BitConverter.ToInt32(data, 0);       
                    }
                    else if (type == typeof(string))
                    {
                        result = System.Text.UnicodeEncoding.Unicode.GetString(data, 0, (int)size).TrimEnd(new char[]{'\0'});
                    }
                    else if (type == typeof(byte[]))
                    {
                        result = data;
                    }
                }
            }
            catch (RegistryException)
            {
                // we will just return null;
            }

            return result;
        }

        public static void GetValueType(IntPtr rootKey, string subKey, string valueName, out Type type, out int size)
        {
            IntPtr key = Registry.OpenKey(rootKey, subKey);
            GetValueType(key, valueName, out type, out size);
            Registry.CloseKey(key);
        }

        public static void GetValueType(IntPtr key, string valueName, out Type type, out int size)
        {
            int typeValue = 0;
            byte[] data = null;
            size = 0;
            int result = Win32.RegQueryValueEx(key, valueName, 0, ref typeValue, data, ref size);

            if (result != (int)ErrorCodes.ERROR_SUCCESS && 
                result != (int)ErrorCodes.ERROR_MORE_DATA)
            {
                throw new RegistryException();
            }

            switch (typeValue)
            {
                case (int)ValueType.REG_DWORD:
                case (int)ValueType.REG_DWORD_BIG_ENDIAN:
                    type = typeof(int);
                    break;
                case (int)ValueType.REG_STRING:
                case (int)ValueType.REG_MULTI_SZ:
                    type = typeof(string);
                    break;
                case (int)ValueType.REG_BINARY:
                default:
                    type = typeof(byte[]);
                    break;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -