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

📄 registrykey.cs

📁 POCKET PC,照片管理系统!提供了真正意义上的目录打开功能
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Diagnostics;
using Addot.Internals;

namespace Addot.Win32
{
    /// <summary>
    /// Microsoft.Win32 implementation for the .Net Compact Framework.
    /// Please, refer to the .Net framework for the complete documentation.
    /// </summary>
    public sealed class RegistryKey : IDisposable
	{
        #region --- Constants ---
        private static readonly int MAX_LENGTH = 255;
        #endregion

        #region --- Fields ---
        private bool IsClosed;
        private uint HKey;
        internal RegistryKey ParentKey;
        private string KeyName;
        #endregion

        #region --- Properties ---
        public string Name
        {
            get
            {
                if( ParentKey != null )
                {
                    return ParentKey.Name + @"\" + KeyName;
                }
                else
                {
                    return KeyName;
                }
            }
        }

        public int SubKeyCount
        {
            get
            {
                int notUsed = 0;
                int subKeys = 0;
                int ret =  NativeRegistry.RegQueryInfoKey(
                                HKey,
                                null,
                                ref notUsed,
                                0,
                                ref subKeys,
                                ref notUsed,
                                ref notUsed,
                                ref notUsed,
                                ref notUsed,
                                ref notUsed,
                                0, 0);

                Debug.WriteLine(string.Format("RegistryKey.RegQueryInfoKey returns: {0}", ret));
                return subKeys;
            }
        }
        public int ValueCount
        {
            get
            {
                int notUsed = 0;
                int values = 0;
                int ret =  NativeRegistry.RegQueryInfoKey(
                    HKey,
                    null,
                    ref notUsed,
                    0,
                    ref notUsed,
                    ref notUsed,
                    ref notUsed,
                    ref values,
                    ref notUsed,
                    ref notUsed,
                    0, 0);

                Debug.WriteLine(string.Format("RegistryKey.RegQueryInfoKey returns: {0}", ret));
                return values;
            }
        }
        #endregion

        internal RegistryKey(uint root, RegistryKey parent, string name)
        {
            IsClosed = false;
            HKey = root;
            ParentKey = parent;
            
            switch( (NativeRegistry.RootKey)root )
            {
                case NativeRegistry.RootKey.HKEY_CLASSES_ROOT:
                    KeyName = @"HKEY_CLASSES_ROOT";
                    break;
                case NativeRegistry.RootKey.HKEY_CURRENT_USER:
                    KeyName = @"HKEY_CURRENT_USER";
                    break;
                case NativeRegistry.RootKey.HKEY_LOCAL_MACHINE:
                    KeyName = @"HKEY_LOCAL_MACHINE";
                    break;
                case NativeRegistry.RootKey.HKEY_USERS:
                    KeyName = @"HKEY_USERS";
                    break;
                default:
                    KeyName = name;
                    break;
            }
        }

        ~RegistryKey()
        {
            Close();
        }

        public RegistryKey OpenSubKey(string name)
        {
            return OpenSubKey(name, false);
        }

        public RegistryKey OpenSubKey(string name, bool writable)
        {
            VerifyInput(name);

            uint newKey = 0;
            int ret = NativeRegistry.RegOpenKeyEx(HKey, name, 0, 0, ref newKey);
            Debug.WriteLine(string.Format("RegistryKey.OpenSubKey returns: {0}", ret));

            if( ret == NativeRegistry.ERROR_SUCCESS )
            {
                IsClosed = false;
                return new RegistryKey(newKey, this, name);
            }
            else
            {
                return null;
            }
        }

        public void Close()
        {
            if ( ( HKey != 0 ) &&
                 ( HKey != (uint)NativeRegistry.RootKey.HKEY_CLASSES_ROOT ) &&
                 ( HKey != (uint)NativeRegistry.RootKey.HKEY_CURRENT_USER ) &&
                 ( HKey != (uint)NativeRegistry.RootKey.HKEY_LOCAL_MACHINE ) &&
                 ( HKey != (uint)NativeRegistry.RootKey.HKEY_USERS ) )
            {
                NativeRegistry.RegCloseKey( HKey );
                HKey = 0;
                IsClosed = true;
            }
        }

        public void SetValue(string name, object val)
        {
            VerifyInput(name);

            int ret = NativeRegistry.ERROR_SUCCESS;

            if( ( val is Int16 ) || ( val is Int32 ) || ( val is UInt16 ) || ( val is UInt32 ) )
            {
                byte[] data = System.BitConverter.GetBytes( (uint)val );
                ret = NativeRegistry.RegSetValueEx( 
                    HKey,
                    name, 
                    0,
                    NativeRegistry.KeyType.REG_DWORD,
                    data,
                    data.Length );
            }
            else if ( val is string )
            {
                byte[] data = System.Text.Encoding.ASCII.GetBytes( (string)val );
                ret = NativeRegistry.RegSetValueEx( 
                    HKey,
                    name,
                    0,
                    NativeRegistry.KeyType.REG_SZ,
                    data,
                    data.Length );
            }
            else if ( val is byte[] )
            {
                ret = NativeRegistry.RegSetValueEx( 
                    HKey,
                    name,
                    0,
                    NativeRegistry.KeyType.REG_BINARY,
                    (byte[])val,
                    ((byte[])val).Length );
            }
            else
            {
                throw new NotSupportedException();
            }

            Debug.WriteLine(string.Format("RegistryKey.SetValue returns: {0}", ret));
        }

        public RegistryKey CreateSubKey(string name)
        {
            VerifyInput(name);

            uint newKey = 0;
            int	security = 0;
            NativeRegistry.KeyDisposition dispo;
            int	ret = NativeRegistry.RegCreateKeyEx(
                                        HKey,
                                        name,
                                        0,
                                        "",
                                        0,
                                        0,
                                        ref security, 
                                        ref newKey,
                                        out dispo );
            Debug.WriteLine(string.Format("RegistryKey.CreateSubKey returns: {0}", ret));

            if ( ret == NativeRegistry.ERROR_SUCCESS )
            {
                return new RegistryKey(newKey, this, name);
            }
            else
            {
                return null;
            }
        }
        public string[] GetSubKeyNames()
        {
            if( IsClosed == true )
            {
                throw new ObjectDisposedException("");
            }

            System.Collections.ArrayList list = new System.Collections.ArrayList();
            string res = "";
            int count = 0;
            while( res != null )
            {
                res = GetKey(count);
                count++;

                if ( res != null )
                {
                    list.Add(res);
                }
            }

            return (string[])list.ToArray(typeof(string));
        }

        public void DeleteSubKey(string name, bool exceptionIfMissing)
        {
            VerifyInput(name);

⌨️ 快捷键说明

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