Logo

C# 添加windows右键菜单

photo

2022年04月28日

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

    }
}

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

热门文章

西游记之大圣归来 《西游记之大圣归来》是根据中国传统神话故事《西游记》进行拓展和演绎的3D动画电影。由横店影视、天空之城、燕城十月与微影时代作为出品方,高路动画、恭梓兄弟、世纪长龙、山东影视、东台龙行盛世、淮安西游产业与永康壹禾作为联合出品方出品,田晓鹏执导,张磊、林子杰、刘九容和童自荣等联袂配音。影片讲述了已于五行山下寂寞沉潜五百年的孙悟空被儿时的唐僧——俗名江流儿的小和尚误打误撞地解除了封印,在相互陪伴的冒险之旅中找回初心,完成自我救赎的故事。 作者:Pastore Antonio
1448 浏览量
常用的Centos操作Shell命令 玩WordPress的时候经常会接触到一些常用的命令,老是忘记……人年纪大了就是不给力啊。浏览量:...常用的Centos操作Shell命令 作者:Pastore Antonio
1387 浏览量
ffmpeg+srs 实现直播流 这篇文章是我在做直播流的时候收集的所有素材,没有太多的整理,都放到了脑袋里面了,之后有时间了再整理成...ffmpeg+srs实现直播流 作者:Pastore Antonio
1386 浏览量
项目十大管理和五大过程 PMBOK五大过程组是什么?PMBOK五大过程组是:启动过程、规划过程、执行过程、监...项目十大管理和五大过程 作者:Pastore Antonio
1363 浏览量
[ Office 365 开发系列 ] 身份认证 前言本文完全原创,转载请说明出处,希望对大家有用。通常我们在开发一个应用时,需要考虑用户身份认证...[Office365开发系列]身份认证 作者:Pastore Antonio
1357 浏览量