Пример #1
0
        /// <summary>
        /// ÅжÏÒ»¸ö×¢²á±íÏîÊÇ·ñ´æÔÚ
        /// </summary>
        /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
        /// <param name="path">×¢²á±í×ÓÏî</param>
        /// <param name="key">¼ü</param>
        /// <returns>ÊÇ·ñ´æÔÚ</returns>
        public static bool Exists(RegistryEnum registryEnum, string path, string key)
        {
            bool        returnValue = false;
            RegistryKey registryKey = null;

            switch (registryEnum)
            {
            case RegistryEnum.LocalMachine: registryKey = Registry.LocalMachine.OpenSubKey(path, false); break;

            case RegistryEnum.ClassesRoot: registryKey = Registry.ClassesRoot.OpenSubKey(path, false); break;

            case RegistryEnum.CurrentConfig: registryKey = Registry.CurrentConfig.OpenSubKey(path, false); break;

            case RegistryEnum.CurrentUser: registryKey = Registry.CurrentUser.OpenSubKey(path, false); break;

            case RegistryEnum.DynData: registryKey = Registry.DynData.OpenSubKey(path, false); break;

            case RegistryEnum.PerformanceData: registryKey = Registry.PerformanceData.OpenSubKey(path, false); break;

            case RegistryEnum.Users: registryKey = Registry.Users.OpenSubKey(path, false); break;
            }

            string[] SubKeyNames = registryKey.GetSubKeyNames();
            for (int i = 0; i < SubKeyNames.Length; i++)
            {
                //Msg.Write(SubKeyNames[i]);
                if (key.Equals(SubKeyNames[i]))
                {
                    returnValue = true;
                    break;
                }
            }
            return(returnValue);
        }
Пример #2
0
        /// <summary>
        /// ¶Á×¢²á±í
        /// </summary>
        /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
        /// <param name="path">·¾¶</param>
        /// <param name="valueName">key</param>
        /// <returns>Öµ</returns>
        public static object Read(RegistryEnum registryEnum, string path, string valueName) {
            RegistryKey key = null;
            switch (registryEnum){
                case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;
                case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;
                case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;
                case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;
                case RegistryEnum.DynData: key = Registry.DynData; break;
                case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;
                case RegistryEnum.Users: key = Registry.Users; break;
            }

            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0) return null;
            for (int x = 0; x < parts.Length; x++) {
                key = key.OpenSubKey(parts[x]);
                if (key.IsNull()) return null;
                if (x == parts.Length - 1) return key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames);
            }
            return null;
        }
Пример #3
0
        /// <summary>
        /// ¶Á×¢²á±í
        /// </summary>
        /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
        /// <param name="path">·¾¶</param>
        /// <param name="valueName">key</param>
        /// <returns>Öµ</returns>
        public static object Read(RegistryEnum registryEnum, string path, string valueName)
        {
            RegistryKey key = null;

            switch (registryEnum)
            {
            case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;

            case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;

            case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;

            case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;

            case RegistryEnum.DynData: key = Registry.DynData; break;

            case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;

            case RegistryEnum.Users: key = Registry.Users; break;
            }

            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0)
            {
                return(null);
            }
            for (int x = 0; x < parts.Length; x++)
            {
                key = key.OpenSubKey(parts[x]);
                if (key.IsNull())
                {
                    return(null);
                }
                if (x == parts.Length - 1)
                {
                    return(key.GetValue(valueName, null, RegistryValueOptions.DoNotExpandEnvironmentNames));
                }
            }
            return(null);
        }
Пример #4
0
        public static string[] GetAllSubKey(RegistryEnum registryEnum, string path)
        {
            RegistryKey key = null;

            switch (registryEnum)
            {
            case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;

            case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;

            case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;

            case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;

            case RegistryEnum.DynData: key = Registry.DynData; break;

            case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;

            case RegistryEnum.Users: key = Registry.Users; break;
            }

            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0)
            {
                return(null);
            }
            for (int x = 0; x < parts.Length; x++)
            {
                key = key.OpenSubKey(parts[x]);
                if (key.IsNull())
                {
                    return(null);
                }
                if (x == parts.Length - 1)
                {
                    return(key.GetSubKeyNames());
                }
            }
            return(new string[0]);
        }
Пример #5
0
        /// <summary>
        /// ɾ³ý×¢²á±í
        /// </summary>
        /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
        /// <param name="path">·¾¶</param>
        /// <param name="valueName">key</param>
        public static void Delete(RegistryEnum registryEnum, string path, string valueName)
        {
            RegistryKey key = null;

            switch (registryEnum)
            {
            case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;

            case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;

            case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;

            case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;

            case RegistryEnum.DynData: key = Registry.DynData; break;

            case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;

            case RegistryEnum.Users: key = Registry.Users; break;
            }

            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0)
            {
                return;
            }
            for (int x = 0; x < parts.Length; x++)
            {
                key = key.OpenSubKey(parts[x], true);
                if (key.IsNull())
                {
                    return;
                }
                if (x == parts.Length - 1)
                {
                    key.DeleteValue(valueName, false);
                }
            }
        }
Пример #6
0
        public static string[] GetAllSubKey(RegistryEnum registryEnum, string path) { 
            RegistryKey key = null;
            switch (registryEnum){
                case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;
                case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;
                case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;
                case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;
                case RegistryEnum.DynData: key = Registry.DynData; break;
                case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;
                case RegistryEnum.Users: key = Registry.Users; break;
            }

            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0) return null;
            for (int x = 0; x < parts.Length; x++) {
                key = key.OpenSubKey(parts[x]);
                if (key.IsNull()) return null;
                if (x == parts.Length - 1) return key.GetSubKeyNames();
            }
            return new string[0];
        }
Пример #7
0
 /// <summary>
 /// ÅжÏÒ»¸ö×¢²á±íÏîÊÇ·ñ´æÔÚ
 /// </summary>
 /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
 /// <param name="path">×¢²á±í×ÓÏî</param>
 /// <param name="key">¼ü</param>
 /// <returns>ÊÇ·ñ´æÔÚ</returns>
 public static bool Exists(RegistryEnum registryEnum, string path, string key) {
     bool returnValue = false; 
     RegistryKey registryKey = null;
     switch (registryEnum){
         case RegistryEnum.LocalMachine: registryKey = Registry.LocalMachine.OpenSubKey(path, false); break;
         case RegistryEnum.ClassesRoot: registryKey = Registry.ClassesRoot.OpenSubKey(path, false); break;
         case RegistryEnum.CurrentConfig: registryKey = Registry.CurrentConfig.OpenSubKey(path, false); break;
         case RegistryEnum.CurrentUser: registryKey = Registry.CurrentUser.OpenSubKey(path, false); break;
         case RegistryEnum.DynData: registryKey = Registry.DynData.OpenSubKey(path, false); break;
         case RegistryEnum.PerformanceData: registryKey = Registry.PerformanceData.OpenSubKey(path, false); break;
         case RegistryEnum.Users: registryKey = Registry.Users.OpenSubKey(path, false); break;
     }
     
     string[] SubKeyNames = registryKey.GetSubKeyNames();
     for (int i = 0; i < SubKeyNames.Length; i++) {
         //Msg.Write(SubKeyNames[i]);
         if (key.Equals(SubKeyNames[i])) {
             returnValue = true;
             break;
         }
     }
     return returnValue;
 }
Пример #8
0
        /// <summary>
        /// ɾ³ý×¢²á±í
        /// </summary>
        /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
        /// <param name="path">·¾¶</param>
        /// <param name="valueName">key</param>
        public static void Delete(RegistryEnum registryEnum, string path, string valueName) {
            RegistryKey key = null;
            switch (registryEnum){
                case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;
                case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;
                case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;
                case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;
                case RegistryEnum.DynData: key = Registry.DynData; break;
                case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;
                case RegistryEnum.Users: key = Registry.Users; break;
            }

            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0) return;
            for (int x = 0; x < parts.Length; x++) {
                key = key.OpenSubKey(parts[x], true);
                if (key.IsNull()) return;
                if (x == parts.Length - 1) key.DeleteValue(valueName, false);
            }
        }
Пример #9
0
        /// <summary>
        /// дע²á±í
        /// </summary>
        /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
        /// <param name="path">·¾¶</param>
        /// <param name="valueName">key</param>
        /// <param name="value">value</param>
        public static void Write(RegistryEnum registryEnum, string path, string valueName, object value) {
            RegistryKey key = null;
            switch (registryEnum){
                case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;
                case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;
                case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;
                case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;
                case RegistryEnum.DynData: key = Registry.DynData; break;
                case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;
                case RegistryEnum.Users: key = Registry.Users; break;
            }
            RegistryKey lastKey = key;
            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0) return;

            for (int x = 0; x < parts.Length; x++) {
                key = key.OpenSubKey(parts[x], true);
                if (key.IsNull()) key = lastKey.CreateSubKey(parts[x]);
                if (x == parts.Length - 1) {
                    if (value is string) {
                        key.SetValue(valueName, value.ToString());
                    } else if (value is uint || value is int || value.GetType().IsEnum) {
                        object o = key.GetValue(valueName, null);

                        if (o.IsNull()) {
                            key.SetValue(valueName, value, RegistryValueKind.DWord);
                        } else {
                            RegistryValueKind kind = key.GetValueKind(valueName);

                            if (kind == RegistryValueKind.DWord) {
                                key.SetValue(valueName, value, RegistryValueKind.DWord);
                            } else if (kind == RegistryValueKind.Binary) {
                                uint num = (uint)value;

                                byte[] b = new byte[4];
                                b[0] = (byte)((num & 0x000000FF) >> 0);
                                b[1] = (byte)((num & 0x0000FF00) >> 1);
                                b[2] = (byte)((num & 0x00FF0000) >> 2);
                                b[3] = (byte)((num & 0xFF000000) >> 3);

                                b[0] = (byte)((num & 0x000000FF) >> 0);
                                b[1] = (byte)((num & 0x0000FF00) >> 8);
                                b[2] = (byte)((num & 0x00FF0000) >> 16);
                                b[3] = (byte)((num & 0xFF000000) >> 24);

                                key.SetValue(valueName, b, RegistryValueKind.Binary);
                            } else if (kind == RegistryValueKind.String) {
                                key.SetValue(valueName, "x" + ((uint)value).ToString("X8"));
                            }
                        }
                    } else if (value is Guid) {
                        key.SetValue(valueName, ((Guid)value).ToString("B"));
                    }
                }

                lastKey = key;
            }

            if (key.IsNotNull()) key.Close();
        }
Пример #10
0
        /// <summary>
        /// дע²á±í
        /// </summary>
        /// <param name="registryEnum">×¢²á±í¸ù¼üö¾Ù</param>
        /// <param name="path">·¾¶</param>
        /// <param name="valueName">key</param>
        /// <param name="value">value</param>
        public static void Write(RegistryEnum registryEnum, string path, string valueName, object value)
        {
            RegistryKey key = null;

            switch (registryEnum)
            {
            case RegistryEnum.LocalMachine: key = Registry.LocalMachine; break;

            case RegistryEnum.ClassesRoot: key = Registry.ClassesRoot; break;

            case RegistryEnum.CurrentConfig: key = Registry.CurrentConfig; break;

            case RegistryEnum.CurrentUser: key = Registry.CurrentUser; break;

            case RegistryEnum.DynData: key = Registry.DynData; break;

            case RegistryEnum.PerformanceData: key = Registry.PerformanceData; break;

            case RegistryEnum.Users: key = Registry.Users; break;
            }
            RegistryKey lastKey = key;

            string[] parts = path.Trim('\\').Split('\\');
            if (parts.IsNull() || parts.Length == 0)
            {
                return;
            }

            for (int x = 0; x < parts.Length; x++)
            {
                key = key.OpenSubKey(parts[x], true);
                if (key.IsNull())
                {
                    key = lastKey.CreateSubKey(parts[x]);
                }
                if (x == parts.Length - 1)
                {
                    if (value is string)
                    {
                        key.SetValue(valueName, value.ToString());
                    }
                    else if (value is uint || value is int || value.GetType().IsEnum)
                    {
                        object o = key.GetValue(valueName, null);

                        if (o.IsNull())
                        {
                            key.SetValue(valueName, value, RegistryValueKind.DWord);
                        }
                        else
                        {
                            RegistryValueKind kind = key.GetValueKind(valueName);

                            if (kind == RegistryValueKind.DWord)
                            {
                                key.SetValue(valueName, value, RegistryValueKind.DWord);
                            }
                            else if (kind == RegistryValueKind.Binary)
                            {
                                uint num = (uint)value;

                                byte[] b = new byte[4];
                                b[0] = (byte)((num & 0x000000FF) >> 0);
                                b[1] = (byte)((num & 0x0000FF00) >> 1);
                                b[2] = (byte)((num & 0x00FF0000) >> 2);
                                b[3] = (byte)((num & 0xFF000000) >> 3);

                                b[0] = (byte)((num & 0x000000FF) >> 0);
                                b[1] = (byte)((num & 0x0000FF00) >> 8);
                                b[2] = (byte)((num & 0x00FF0000) >> 16);
                                b[3] = (byte)((num & 0xFF000000) >> 24);

                                key.SetValue(valueName, b, RegistryValueKind.Binary);
                            }
                            else if (kind == RegistryValueKind.String)
                            {
                                key.SetValue(valueName, "x" + ((uint)value).ToString("X8"));
                            }
                        }
                    }
                    else if (value is Guid)
                    {
                        key.SetValue(valueName, ((Guid)value).ToString("B"));
                    }
                }

                lastKey = key;
            }

            if (key.IsNotNull())
            {
                key.Close();
            }
        }