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 添加自定义右键菜单

        /// <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>
        /// 从curKeySet下获取subkey
        /// </summary>
        /// <param name="curKeySet"></param>
        /// <param name="subKey"></param>
        /// <param name="autoCreate">不存在时是否创建</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 示例:为所有类型文件以及文件夹,添加系统右键shell菜单
        /// </summary>
        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

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值