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

    }
}