C# 添加windows右键菜单

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sci
{
    public class RegistryTool
    {
        #region 添加自定义右键菜单

        /// 
        /// 在widows系统中为指定类型的文件或文件夹,添加自定义右键菜单
        /// 
        /// 文件拓展名如:".txt" 为所有类型的文件添加为 "*" 
        /// 内部id
        /// 显示名称
        /// 发送命令到指定的exe
        /// 标识当前菜单项的自定义参数
        /// 是否为文件夹
        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);  // 添加命令内容
        }

        /// 
        /// 从curKeySet下获取subkey
        /// 
        /// 
        /// 
        /// 不存在时是否创建
        /// 
        private 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;
        }

        /// 
        /// 示例:为所有类型文件以及文件夹,添加系统右键shell菜单
        /// 
        public static void AddContextMenu(string exePath)
        {
            string ToolName = "ContextIteamName";
            //string exePath = Application.ExecutablePath;

            // 为所有类型的文件添加 系统菜单
            RegistryTool.AddWindowsContextMenu("*", ToolName, "自定义右键菜单1", exePath, null);

            // 为文件夹添加 系统菜单
            RegistryTool.AddWindowsContextMenu(null, ToolName, "自定义右键菜单1", exePath, null, true);
        }

        #endregion

    }
}

你可能感兴趣的:(C#,添加windows右键菜单,注册表操作)