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

    }
}

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

热门文章

修复群晖Synology Drive client右键菜单缺失问题 本教程主要解决windows10右键菜单中没有SynologyDrive菜单的问题,整体思路是找到...修复群晖SynologyDriveclient右键菜单缺失问题 作者:Pastore Antonio
1811 浏览量
docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 前言:有时候我们会在docker上下载一个镜像,或者是上传一个镜像到docker上,甚至有时候就是在...docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 作者:Pastore Antonio
1788 浏览量
configure: error: Package requirements (oniguruma) were not met configure:error:Packagerequirements(oniguruma)...configure:error:Packagerequirements(oniguruma)werenotmet 作者:Pastore Antonio
1528 浏览量
Adobe Acrobat Pro 激活 这里记录了一些AdobeAcrobat的激活教程和组件。浏览量:1,680 作者:Pastore Antonio
1525 浏览量
追寻日出,找回自己 为什么我要去追寻日出?其实我是一个很懒的人,每次都起不来,直到有一次我在租房中睡到了大天亮,阳光照...追寻日出,找回自己 作者:Pastore Antonio
1505 浏览量