注册表

本文介绍了一个用于操作Windows注册表的抽象类RegistryAbstract,提供了创建、读取、删除注册表键值的方法,并列举了多个常用的注册表路径。
using System;
using System.Diagnostics;
using Microsoft.Win32;

namespace Viewer
{
    public abstract class RegistryAbstract
    {
        public const string HkcuRun = @"Software\Microsoft\Windows\CurrentVersion\Run";
        public const string ScreenSave = @"Control Panel\Desktop";
        public const string HklmWinlogon = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon";
        public const string HklmCommandProcessor = @"Software\Microsoft\Command Processor";
        public const string HklmLoad = @"Software\Microsoft\Windows NT\CurrentVersion\Windows";
        public const string HklmWinlogonNotify = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify";
        public const string SharedTaskScheduler = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SharedTaskScheduler";
        public const string HklmShellExecuteHooks = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks";
        public const string PrintMonitors = @"SYSTEM\CurrentControlSet\Control\Print\Monitors";
        public const string PrintProviders = @"SYSTEM\CurrentControlSet\Control\Print\Providers";

        public const string KnownDLLs = @"SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs";
        public const string ShellServiceObjectDelayLoad = @"SOFTWARE\Microsoft\Windows\CurrentVersion\ShellServiceObjectDelayLoad";
        public const string HklmApproved = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved";
        public const string HklmIEPlug = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Ext\PreApproved";
        public const string HklmIEPlug2 = @"Software\Microsoft\Windows\CurrentVersion\Ext\Stats";
        public const string RightMenu1 = @"SOFTWARE\Classes\*\shellex\ContextMenuHandlers";
        public const string RightMenu2 = @"SOFTWARE\Classes\AllFilesystemObjects\shellex\ContextMenuHandlers";
        public const string RightMenu3 = @"SOFTWARE\Classes\Folder\ShellEx\ContextMenuHandlers";
        public const string InstalledComponents = @"SOFTWARE\Microsoft\Active Setup\Installed Components";
        public const string BHO = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects";
        public const string Services = @"SYSTEM\CurrentControlSet\services";

        public const string MingPath = @"Software\Ming\Viewer\Config";

        private const string PositionReg = @"Software\Microsoft\Windows\CurrentVersion\Applets\Regedit";

        /// <summary>
        /// 在指定键下创建或者设置指定值
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="regPath"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="regValKind"></param>
        public virtual void SetRegistryValue(RegistryKey key, string subKey, string name, object value, RegistryValueKind kind)
        {
            RegistryKey reg = key.CreateSubKey(subKey, RegistryKeyPermissionCheck.ReadWriteSubTree);
            if (reg != null)
            {
                reg.SetValue(name, value, kind);
                reg.Close();
            }
        }

        /// <summary>
        /// 移除指定键下的指定名称
        /// </summary>
        /// <param name="key"></param>
        /// <param name="subKey"></param>
        /// <param name="name"></param>
        public virtual void DeleteRegistryValue(RegistryKey key, string subKey, string name)
        {
            RegistryKey reg = key.OpenSubKey(subKey, true);
            if (reg != null)
            {
                reg.DeleteValue(name, false);
                reg.Close();
            }
        }

        public virtual void DeleteRegistrySubKey(RegistryKey key, string subKey, string subKeyName)
        {
            RegistryKey reg = key.OpenSubKey(subKey, true);
            if (reg != null)
            {
                reg.DeleteSubKey(subKeyName);
                reg.Close();
            }
        }

        public virtual void DeleteRegistrySubKeyTree(RegistryKey key, string subKey, string subKeyTreeName)
        {
            RegistryKey reg = key.OpenSubKey(subKey, true);
            if (reg != null)
            {
                reg.DeleteSubKeyTree(subKeyTreeName);
                reg.Close();
            }
        }

        public static void PositionRegedit(string subKey)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(PositionReg, RegistryKeyPermissionCheck.ReadWriteSubTree);
            if (key != null)
            {
                key.SetValue("LastKey", "我的电脑\\" + subKey, RegistryValueKind.String);
                key.Close();
                Process[] p = Process.GetProcessesByName("regedit");
                foreach (Process ps in p) ps.Kill();
                Process.Start("regedit.exe");
            }
        }

        /// <summary>
        /// 定位注册表
        /// </summary>
        /// <param name="regPath"></param>
        public static void PositionRegedit7(string subKey)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(PositionReg, RegistryKeyPermissionCheck.ReadWriteSubTree);
            if (key != null)
            {
                key.SetValue("LastKey", "计算机\\" + subKey, RegistryValueKind.String);
                key.Close();
                Process[] p = Process.GetProcessesByName("regedit");
                foreach (Process ps in p) ps.Kill();
                Process.Start("regedit.exe");
            }
        }

        /// <summary>
        /// 获取指定键下面的所有的子键
        /// </summary>
        /// <param name="key"></param>
        /// <param name="subKey"></param>
        /// <returns></returns>
        protected virtual string[] GetRegistrySubKeys(RegistryKey key, string subKey)
        {
            string[] subs = null;
            RegistryKey reg = key.OpenSubKey(subKey);
            if (reg != null)
            {
                subs = reg.GetSubKeyNames();
                reg.Close();
            }
            return subs;
        }

        /// <summary>
        /// 获取指定键下面的所有名称
        /// </summary>
        /// <param name="key"></param>
        /// <param name="subKey"></param>
        /// <returns></returns>
        protected virtual string[] GetRegistryNames(RegistryKey key, string subKey)
        {
            string[] names = null;
            RegistryKey reg = key.OpenSubKey(subKey);
            if (reg != null)
            {
                names = reg.GetValueNames();
                reg.Close();
            }
            return names;
        }

        /// <summary>
        /// 获取指定键下的指定名称的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="subKey"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public virtual object GetRegistryNameValue(RegistryKey key, string subKey, string name)
        {
            object value = null;
            RegistryKey reg = key.OpenSubKey(subKey);
            if (reg != null)
            {
                value = reg.GetValue(name, 0);
                reg.Close();
            }
            return value;
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值