注册表操作类【原】

using System;

using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace SwireBev.Mobile.Utility
{
    public class RegeditUtility
    {
        /// <summary>
        
/// 键域
        
/// </summary>
        private RegistryKey rKey = null;
        /// <summary>
        
/// 键值类型
        
/// </summary>
        private RegistryValueKind rValueKind = RegistryValueKind.Binary;
        /// <summary>
        
/// 注册表操作类
        
/// </summary>
        
/// <param name="key">键域</param>
        
/// <param name="vauleKing">键值类型</param>
        public RegeditUtility(string key, string vauleKind)
        {
            //HKEY_LOCAL_MACHINE
            
//HKEY_CLASSES_ROOT
            
//HKEY_CURRENT_USER
            
//HKEY_USERS

            
//Binary
            
//DWord
            
//ExpandString
            
//MultiString
            
//QWord
            
//String
            
//Unknown
            switch (key)
            {
                case "HKEY_LOCAL_MACHINE":
                    {
                        rKey = Registry.LocalMachine;
                        break;
                    }
                case "HKEY_CLASSES_ROOT":
                    {
                        rKey = Registry.ClassesRoot;
                        break;
                    }
                case "HKEY_CURRENT_USER":
                    {
                        rKey = Registry.CurrentUser;
                        break;
                    }
                case "HKEY_USERS":
                    {
                        rKey = Registry.Users;
                        break;
                    }
                default:
                    {
                        rKey = Registry.LocalMachine;
                        break;
                    }
            }

            switch (vauleKind)
            {
                case "Binary":
                    {
                        rValueKind = RegistryValueKind.Binary;
                        break;
                    }
                case "DWord":
                    {
                        rValueKind = RegistryValueKind.DWord;
                        break;
                    }
                case "ExpandString":
                    {
                        rValueKind = RegistryValueKind.ExpandString;
                        break;
                    }
                case "MultiString":
                    {
                        rValueKind = RegistryValueKind.MultiString;
                        break;
                    }
                case "QWord":
                    {
                        rValueKind = RegistryValueKind.QWord;
                        break;
                    }
                case "String":
                    {
                        rValueKind = RegistryValueKind.String;
                        break;
                    }
                case "Unknown":
                    {
                        rValueKind = RegistryValueKind.Unknown;
                        break;
                    }
                default:
                    {
                        rValueKind = RegistryValueKind.String;
                        break;
                    }
            }
        }
        
        /// <summary>
        
/// 读取指定名称的注册表的值
        
/// </summary>
        
/// <param name="name">路径</param>
        
/// <param name="key">键值名</param>
        
/// <returns></returns>
        public string GetRegistValue(string name, string key)
        {
            string registData = "";
            RegistryKey software = rKey.OpenSubKey(name, true);
            if (software != null)
            {
                object o = software.GetValue(key);
                if (o != null)
                {
                    registData = o.ToString();
                }
                software.Close();
            }
            return registData;
        }

        /// <summary>
        
/// 向注册表中写数据
        
/// </summary>
        
/// <param name="name">路径</param>
        
/// <param name="key">键名</param>
        
/// <param name="value">键值</param>
        public void SetRegistValue(string name, string key, string value)
        {
            RegistryKey software = rKey.OpenSubKey(name, true);
            if (software != null)
            {
                if (value.Equals(""))
                {
                    RegistryKey aimdir = software.OpenSubKey(key);
                    if (aimdir == null)
                    {
                        software.CreateSubKey(key);
                    }
                    else
                    {
                        aimdir.Close();
                    }
                }
                else
                {
                    software.SetValue(key, value, rValueKind);
                }
            }
            software.Close();
        }

        /// <summary>
        
/// 删除注册表中指定的注册表项
        
/// </summary>
        
/// <param name="name">路径</param>
        
/// <param name="key">键名</param>
        
/// <param name="subKey">是否删除键值(true 删除键值 false 删除 文件项)</param>
        public void DeleteRegist(string name, string key, bool subKey)
        {
            //string[] aimnames;
            RegistryKey software = rKey.OpenSubKey(name, true);
            if (software != null)
            {
                if (subKey.Equals(true))
                {
                    software.DeleteValue(key);
                }
                else
                {
                    software.DeleteSubKey(key, false);
                }
            }
            software.Close();
        }

        /// <summary>
        
/// 判断指定注册表项是否存在
        
/// </summary>
        
/// <param name="name">路径</param>
        
/// <param name="key">键名</param>
        
/// <returns></returns>
        public bool IsRegeditExit(string name, string key)
        {
            bool _exit = false;
            RegistryKey software = null;
            try
            {
                string[] subkeyNames;
                software = rKey.OpenSubKey(name, true);
                if (software != null)
                {
                    subkeyNames = software.GetValueNames();
                    foreach (string keyName in subkeyNames)
                    {
                        if (keyName == key)
                        {
                            _exit = true;
                            return _exit;
                        }
                    }
                    software.Close();
                }
            }
            catch
            { }
            finally
            {
                if (software != null)
                {
                    software.Close();
                }
            }
            return _exit;
        }
    }
}
posted @ 2012-10-31 15:59  小さいです哥  阅读(259)  评论(0编辑  收藏  举报