using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sci
{
#region Software注册表操作
/// <summary>
/// HKEY_CURRENT_USER\Software\注册表操作
/// </summary>
public class RegistrySoft
{
private static RegistryKey curKeySet = RegistryTool.GetSubkey(Registry.CurrentUser, @"Software", true);
//string subKey = "Sci\\FileSplit\\Set";
/// <summary>
/// 判断subkey注册表KeySet是否存在
/// </summary>
/// <param name="subkey"></param>
/// <returns></returns>
public static bool CotainsSubkey(string subkey)
{
return RegistryTool.CotainsSubkey(curKeySet, subkey);
}
/// <summary>
/// 获取subkey对应的KeySet
/// </summary>
/// <param name="subkey"></param>
/// <param name="autoCreate">不存在时,是否自动创建</param>
public static void GetSubkey(string subkey, bool autoCreate = false)
{
RegistryTool.GetSubkey(curKeySet, subkey, autoCreate);
}
/// <summary>
/// 删除subkey对应的注册表树
/// </summary>
/// <param name="subkey"></param>
public static void DeletSubkey(string subkey)
{
RegistryTool.DeleteSubkey(curKeySet, subkey);
}
/// <summary>
/// 判断subkey路径下name对应的键值对是否存在
/// </summary>
/// <param name="subkey"></param>
/// <param name="name"></param>
/// <returns></returns>
public static bool CotainsValue(string subkey, string name)
{
return RegistryTool.CotainsValue(curKeySet, subkey, name);
}
/// <summary>
/// 在subkey路径下添加 name, value 键值对
/// </summary>
/// <param name="subkey"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public static void SetValue(string subkey, string name, object value)
{
RegistryTool.SetValue(curKeySet, subkey, name, value);
}
/// <summary>
/// 获取subkey路径下,名称为name键对应的值
/// </summary>
/// <param name="subkey"></param>
/// <param name="name"></param>
/// <returns></returns>
public static object GetValue(string subkey, string name)
{
return RegistryTool.GetValue(curKeySet, subkey, name);
}
/// <summary>
/// 获取subkey路径下,名称为name键对应的值的字符串形式
/// </summary>
/// <param name="subkey"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string GetValueStr(string subkey, string name)
{
return RegistryTool.GetValueStr(curKeySet, subkey, name);
}
/// <summary>
/// 删除subkey路径下,键名为name的注册表信息
/// </summary>
/// <param name="subkey"></param>
/// <param name="name"></param>
public static void DeleteValue(string subkey, string name)
{
RegistryTool.DeleteValue(curKeySet, subkey, name);
}
}
#endregion
#region 注册表操作逻辑
public class RegistryTool
{
//=======================================================
// 注册表操作
//=======================================================
# region 注册表操作
/// <summary>
/// 判断注册表是否含有子键subkey
/// <summary>
public static bool CotainsSubkey(RegistryKey curKeySet, string subkey)
{
RegistryKey subSet = curKeySet.OpenSubKey(subkey, true);
return (subSet != null);
}
/// <summary>
/// 从curKeySet下获取subkey
/// </summary>
/// <param name="curKeySet"></param>
/// <param name="subKey"></param>
/// <param name="autoCreate">不存在时是否创建</param>
/// <returns></returns>
public static RegistryKey GetSubkey(RegistryKey curKeySet, string subKey, bool autoCreate = false)
{
RegistryKey subSet = curKeySet.OpenSubKey(subKey, true);
if (subSet == null && autoCreate) subSet = curKeySet.CreateSubKey(subKey);
return subSet;
}
/// <summary>
/// 删除注册表subkey
/// <summary>
public static void DeleteSubkey(RegistryKey curKeySet, string subkey)
{
RegistryKey subSet = curKeySet.OpenSubKey(subkey, true);
if (subSet != null) subSet.DeleteSubKeyTree(subkey); //删除注册表信息
}
/// <summary>
/// 记录键值数据到注册表subkey = @"Scimence\Email\Set";
/// </summary>
public static void SetValue(RegistryKey curKeySet, string subkey, string name, object value)
{
RegistryKey subSet = GetSubkey(curKeySet, subkey, true);
subSet.SetValue(name, value); //保存键值数据
}
/// <summary>
/// 获取注册表subkey下,键name的值
/// <summary>
public static object GetValue(RegistryKey curKeySet, string subkey, string name)
{
RegistryKey subSet = curKeySet.OpenSubKey(subkey, true);
return (subSet == null ? null : subSet.GetValue(name, null));
}
/// <summary>
/// 获取注册表subkey下,键name的字符串值
/// <summary>
public static string GetValueStr(RegistryKey curKeySet, string subkey, string name)
{
object value = GetValue(curKeySet, subkey, name);
return value == null ? "" : value.ToString();
}
/// <summary>
/// 判断注册表subkey下,是否含有name键值信息
/// <summary>
public static bool CotainsValue(RegistryKey curKeySet, string subkey, string name)
{
RegistryKey subSet = curKeySet.OpenSubKey(subkey, true);
if (subSet == null) return false;
else return subSet.GetValueNames().Contains<string>(name);
}
/// <summary>
/// 删除注册表subkey下,name键值信息
/// <summary>
public static void DeleteValue(RegistryKey curKeySet, string subkey, string name)
{
RegistryKey subSet = curKeySet.OpenSubKey(subkey, true);
if (subSet != null) subSet.DeleteValue(name, false);
}
# endregion
#region 添加或删除,开机启动项目
/// <summary>
/// 添加开机启动项,如:AddStartRun("QQ", "C:\\windows\\system32\\QQ.exe");
/// </summary>
/// <param name="name">启动项名称</param>
/// <param name="exePath">文件启动路径</param>
public static void AddStartRun(string name, string exePath)
{
string path = "\"" + exePath + "\"";
SetValue(Registry.CurrentUser, @"Software\Microsoft\Windows\CurrentVersion\Run", name, path);
}
/// <summary>
/// 移除开机启动注册表项
/// </summary>
/// <param name="name"></param>
public static void RemoveStartRun(string name)
{
if (!name.Trim().Equals(""))
{
DeleteValue(Registry.CurrentUser, @"Software\Microsoft\Windows\CurrentVersion\Run", name);
}
}
#endregion
#region 添加文件拓展名关联
//Windows Registry Editor Version 5.00
//[HKEY_CLASSES_ROOT\.split]
//@="split_auto_file"
//[HKEY_CLASSES_ROOT\split_auto_file]
//@="FileSplit文件碎片(集齐所有碎片至同一个文件夹下,双击任意碎片即可进行文件还原)"
//[HKEY_CLASSES_ROOT\split_auto_file\shell]
//[HKEY_CLASSES_ROOT\split_auto_file\shell\open]
//[HKEY_CLASSES_ROOT\split_auto_file\shell\open\command]
//@="\"E:\\程序\\FileSplit\\FileSplit\\FileSplitFunc\\FileSplitFunc\\bin\\Debug\\FileSplit.exe\" \"%1\""
/// <summary>
/// 为文件拓展名ext,添加默认打开方式exePath
/// </summary>
/// <param name="ext">文件拓展名,如: .mp4</param>
/// <param name="exePath">exe文件路径</param>
/// <param name="dexcription">拓展名文件描述信息</param>
public static void AddExtenOpenWith(string ext, string exePath, string description = "")
{
// 添加拓展名关联字段
RegistryKey extSet = GetSubkey(Registry.ClassesRoot, ext, true);
string extAutoFile = extSet.GetValue("", "").ToString().Trim();
if (extAutoFile.Equals(""))
{
extAutoFile = ext.Trim('.').ToLower() + "_auto_file";
extSet.SetValue("", extAutoFile);
}
// 添加描述信息
RegistryKey AutoFileSet = GetSubkey(Registry.ClassesRoot, extAutoFile, true);
string regDescription = AutoFileSet.GetValue("", "").ToString().Trim();
if (!regDescription.Equals(description)) AutoFileSet.SetValue("", description);
// 添加关联exe文件路径
RegistryKey OpenCommandSet = GetSubkey(AutoFileSet, @"shell\open\command", true);
string regPath = OpenCommandSet.GetValue("", "").ToString().Trim();
string setPath = "\"" + exePath + "\"" + " " + "\"" + "%1" + "\"";
if (!regPath.Equals(setPath)) OpenCommandSet.SetValue("", setPath);
}
#endregion
#region 自定义URL启动协议(类似http:// 或 https:// 或 ftp://)
// 示例:
// RegistryTool.AddUrlProtocol("exearg", @"E:\程序\exeArgs\exeArgs.exe");
// System.Diagnostics.Process.Start("exearg://自定义测试参数");
// "E:\程序\exeArgs\exeArgs.exe"被启动,并且接收到启动参数: "exearg://自定义测试参数/"
//--------------------
//Windows Registry Editor Version 5.00
//[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\exearg]
//@="自定义exearg:// 协议"
//"URL Protocol"="该字段将exearg与URL关联"
//[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\exearg\DefaultIcon]
//@="\"E:\\程序\\exeArgs\\exeArgs.exe\",1"
//[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\exearg\shell]
//[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\exearg\shell\open]
//[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\exearg\shell\open\command]
//@="\"E:\\程序\\exeArgs\\exeArgs.exe\" \"%1\""
//--------------------
/// <summary>
/// 为文件拓展名ext,添加默认打开方式exePath
/// </summary>
/// <param name="protocolName">协议名称,如: http、https、ftp、weixin</param>
/// <param name="exePath">exe文件路径</param>
/// <param name="dexcription">协议描述信息</param>
public static void AddUrlProtocol(string protocolName, string exePath, string description = "")
{
// 添加Protocol协议
RegistryKey regSet = GetSubkey(Registry.ClassesRoot, protocolName, true);
// 添加描述信息
string regDescription = regSet.GetValue("", "").ToString().Trim();
if (regDescription.Equals(""))
{
if (description.Equals("")) description = "自定义"+ protocolName +":// 协议";
regSet.SetValue("", description);
}
// 添加"URL Protocol"字段
string regURL = regSet.GetValue("URL Protocol", "").ToString().Trim();
if (regURL.Equals("")) regSet.SetValue("URL Protocol", "该字段将" + protocolName + "与URL关联");
// 添加关联图标
RegistryKey IconSet = GetSubkey(regSet, @"DefaultIcon", true);
string iconPath = IconSet.GetValue("", "").ToString().Trim();
string setIconPath = "\"" + exePath + "\"" + ",1" ;
if (!iconPath.Equals(setIconPath)) IconSet.SetValue("", setIconPath);
// 添加关联exe文件路径
RegistryKey OpenCommandSet = GetSubkey(regSet, @"shell\open\command", true);
string regPath = OpenCommandSet.GetValue("", "").ToString().Trim();
string setPath = "\"" + exePath + "\"" + " " + "\"" + "%1" + "\"";
if (!regPath.Equals(setPath)) OpenCommandSet.SetValue("", setPath);
}
#endregion
#region Windows文件浏览器,添加自定义右键菜单
/// <summary>
/// 在widows系统中为指定类型的文件或文件夹,添加自定义右键菜单
/// </summary>
/// <param name="fileExten">文件拓展名如:".txt" 为所有类型的文件添加为 "*" </param>
/// <param name="menuId">内部id</param>
/// <param name="menuName">显示名称</param>
/// <param name="exePath">发送命令到指定的exe</param>
/// <param name="callBackArg">标识当前菜单项的自定义参数</param>
/// <param name="isDirectory">是否为文件夹</param>
public static void AddWindowsContextMenu(string fileExten, string menuId, string menuName, string exePath, string callBackArg = null, bool isDirectory = false)
{
exePath = "\"" + exePath +"\"";
string shellPath = (isDirectory ? "Directory" : fileExten) + @"\shell\"; // 为指定类型的文件添加右键shell菜单,或为文件夹添加右键菜单
RegistryKey shellSet = GetSubkey(Registry.ClassesRoot, shellPath, true);
//Registry.ClassesRoot.OpenSubKey(shellPath, true);
//if (shellSet == null)
//{
// Registry.ClassesRoot.CreateSubKey(shellPath);
// shellSet = Registry.ClassesRoot.OpenSubKey(shellPath, true);
//}
RegistryKey menuSet = GetSubkey(shellSet, menuId, true); // 创建Menu菜单项
if (!menuSet.GetValue("", "").ToString().Equals(menuName)) menuSet.SetValue("", menuName); // 菜单项显示名称
if (!menuSet.GetValue("icon", "").ToString().Equals(exePath)) menuSet.SetValue("icon", exePath); // 菜单项显示图标
RegistryKey commandSet = GetSubkey(menuSet, "command", true); // 添加command命令Set
string commandInfo = exePath + (callBackArg == null ? "" : (" \"" + callBackArg + "\" ")) + " \"%1\""; // %1为系统传递的文件或文件夹完整路径信息
if (!commandSet.GetValue("", "").ToString().Equals(commandInfo)) commandSet.SetValue("", commandInfo); // 添加命令内容
}
/// <summary>
/// 示例:为所有类型文件以及文件夹,添加系统右键shell菜单
/// </summary>
public static void AddContextMenu(string exePath)
{
string ToolName = "FileSystem";
//string exePath = Application.ExecutablePath;
// 为所有类型的文件添加 系统菜单
RegistryTool.AddWindowsContextMenu("*", ToolName + ".add", "添加到FileSystem", exePath, null);
//RegistryTool.AddWindowsContextMenu("Folder", ToolName + ".export", "从FileSystem中导出", exePath, "export");
RegistryTool.AddWindowsContextMenu("*", ToolName + ".delet", "从FileSystem中删除", exePath, "delet");
// 为文件夹添加 系统菜单
RegistryTool.AddWindowsContextMenu(null, ToolName + ".add", "添加到FileSystem", exePath, null, true);
RegistryTool.AddWindowsContextMenu(null, ToolName + ".export", "从FileSystem中导出", exePath, "export", true);
RegistryTool.AddWindowsContextMenu(null, ToolName + ".delet", "从FileSystem中删除", exePath, "delet", true);
}
#endregion
}
#endregion
}
Software注册表操作
最新推荐文章于 2024-05-12 22:10:44 发布