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;
}
}
}
本文介绍了一个用于操作Windows注册表的抽象类RegistryAbstract,提供了创建、读取、删除注册表键值的方法,并列举了多个常用的注册表路径。
1233

被折叠的 条评论
为什么被折叠?



