阅读背景:

为程序添加系统上下文菜单

来源:互联网 
using System;
using System.Diagnostics;
using Microsoft.Win32;

namespace SimpleContextMenu
{
    /// <summary>
    /// 在注册表中注册和注销文件上下文菜单.
    /// </summary>
    static class FileShellExtension
    {
        /// <summary>
        /// 注册上下文菜单
        /// </summary>
        /// <param name="fileType">要注册的文件类型</param>
        /// <param name="shellKeyName">在注册表中显示的名称</param>
        /// <param name="menuText">在上下文菜单中显示的文字</param>
        /// <param name="menuCommand">被执行的命令行</param>
        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);
            }
        }

        /// <summary>
        /// 注销上下文菜单.
        /// </summary>
        /// <param name="fileType">注销的文件类型</param>
        /// <param name="shellKeyName">在注册表中注册的名称</param>
        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.Diagnostics;
using M



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: