博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为程序添加系统上下文菜单
阅读量:6973 次
发布时间:2019-06-27

本文共 2218 字,大约阅读时间需要 7 分钟。

using System;using System.Diagnostics;using Microsoft.Win32;namespace SimpleContextMenu{    ///     /// 在注册表中注册和注销文件上下文菜单.    ///     static class FileShellExtension    {        ///         /// 注册上下文菜单        ///         /// 要注册的文件类型        /// 在注册表中显示的名称        /// 在上下文菜单中显示的文字        /// 被执行的命令行        public static void Register(string fileType, string shellKeyName, string menuText, string menuCommand)        {            Debug.Assert(!string.IsNullOrEmpty(fileType) &&                !string.IsNullOrEmpty(shellKeyName) &&                !string.IsNullOrEmpty(menuText) &&                !string.IsNullOrEmpty(menuCommand));            //创建注册表位置的完整路径            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);            //注册表中添加上下文菜单            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))            {                key.SetValue(null, menuText);            }            //添加命令到被调用的注册表            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(string.Format(@"{0}\command", regPath)))            {                key.SetValue(null, menuCommand);            }        }        ///         /// 注销上下文菜单.        ///         /// 注销的文件类型        /// 在注册表中注册的名称        public static void Unregister(string fileType, string shellKeyName)        {            Debug.Assert(!string.IsNullOrEmpty(fileType) && !string.IsNullOrEmpty(shellKeyName));            // 注册表中的位置的完整路径	            string regPath = string.Format(@"{0}\shell\{1}", fileType, shellKeyName);            //从注册表中删除上下文菜单            Registry.ClassesRoot.DeleteSubKeyTree(regPath);        }    }}

调用方法:

using System;using System.Windows.Forms;using System.IO;using System.Drawing;using System.Drawing.Imaging;[assembly: CLSCompliant(true)]namespace SimpleContextMenu{    static class Program    {               const string FileType = "Directory";          // 注册的文件类型        const string KeyName = "Simple Context Menu"; //在注册表的上下文菜单名称        const string MenuText = "Copy to Grayscale";  // 上下文菜单文本        [STAThread]        static void Main(string[] args)        {            // 注册右键菜单            if (!ProcessCommand(args))            {                //接收命令行参数处理            }        }	}}
原文地址:

转载地址:http://azrsl.baihongyu.com/

你可能感兴趣的文章
【2011.9.20】基于CXF Web Service:Apache CXF简单部署 .
查看>>
jquery Flexigrid的使用
查看>>
Inotify + rsync
查看>>
详解JDBC驱动的四种类型
查看>>
1.jQuery基础语法 2.jQuery选择器、操作页面文档元素 3.jqueryDOM操作 4.jqueryCSS操作 5.Jquery事件 6.Jquery动画...
查看>>
用于软件包管理的21个Linux YUM命令 转载
查看>>
中风从水治案
查看>>
图像处理之基础---一维小波变换,可多次分解
查看>>
SQL Server 内存使用量下降问题
查看>>
泛型中的类型约束和类型推断
查看>>
嵌入式驱动开发之dsp fpga通信接口---spi串行外围接口、emif sram接口
查看>>
网络协议之socks---子网和公网的穿透
查看>>
Java控制语句——if语句
查看>>
BadUSB的防范研究
查看>>
struts2中从数据库中读取数据,并在JSP页面中遍历保存有JavaBean对象的List对象
查看>>
网站flash黑屏问题
查看>>
JAVA TIMER定时器
查看>>
GitHub 实现多人协同提交代码并且权限分组管理
查看>>
解决vsftpd乱码
查看>>
CCF-201512-3 绘图
查看>>