在資源管理器中添加自定義右鍵菜單c#

如果我們需要在資源管理器中選中文件後右鍵出現自定義右鍵菜單,我們可以通過修改註冊表的形式實現

下面例子僅供提供思路

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;

namespace MenuTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = System.Environment.CurrentDirectory + "\\MenuTest.exe" + @" \MenuName" + @" \Menu" + " %1";//當前exe文件夾路徑
            if (args.Length <= 0)
            {
                AddFileContextMenuItem("Menu", str);
                Recod(@"E:\Menu.txt", "註冊右鍵菜單");
            }
            else
            {
                if (args.Length > 0 && args[0] != "")
                {
                    Recod(@"E:\Menu.txt", "第一個參數爲" + args[0].ToString() + "\r\n 第二個參數爲" + args[1].ToString() + "\r\n 第三個參數爲" + args[2].ToString() + "數組長度爲" + args.Length);
                }
                else
                {
                    Recod(@"E:\Menu.txt", "args參數沒有數據");
                }
            }
            //刪除註冊表
            //DeleteContextMenu("Menu");
        }
        /// <summary>
        /// 註冊右鍵菜單
        /// </summary>
        /// <param name="itemName">右鍵菜單名稱</param>
        /// <param name="assoCreatedProgramFullPath">程序所在路徑</param>
        private static void AddFileContextMenuItem(string itemName, string assoCreatedProgramFullPath)
        {
            try
            {
                //註冊到所有文件
                RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell", true);
                //註冊到所有目錄
                // RegistryKey shell = Registry.ClassesRoot.OpenSubKey(@"directory\shell", true);
                //註冊到文件夾
                //RegistryKey shell = Registry.ClassesRoot.OpenSubKey("directory", true).OpenSubKey("shell", true);
                if (shellKey == null)
                {
                    shellKey = Registry.ClassesRoot.CreateSubKey(@"*\shell");
                }
                RegistryKey rightCommondKey = shellKey.OpenSubKey(itemName);
                if (rightCommondKey == null)
                {
                    rightCommondKey = shellKey.CreateSubKey(itemName);
                }
                RegistryKey assoCreatedProgramKey = rightCommondKey.CreateSubKey("command");
                assoCreatedProgramKey.SetValue(string.Empty, assoCreatedProgramFullPath);
                assoCreatedProgramKey.Close();
                rightCommondKey.Close();
                shellKey.Close();
            }
            catch (Exception ex)
            {

                Recod(@"E:\Menu.txt", "註冊右鍵菜單異常");
            }
        }
        /// <summary>
        /// 記錄日誌
        /// </summary>
        /// <param name="noteName"></param>
        /// <param name="content"></param>
        private static void Recod(string noteName, string content)
        {
            try
            {
                FileStream fs = new FileStream(@noteName, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.Begin);
                sw.WriteLine(content);
                sw.Close();
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 刪除菜單
        /// </summary>
        /// <param name="itemName"></param>
        private static void DeleteContextMenu(string itemName)
        {
            try
            {

                RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell", true);
                RegistryKey rightCommondKey = shellKey.OpenSubKey(itemName, true);
                if (rightCommondKey != null)
                {
                    rightCommondKey.DeleteSubKeyTree("");
                }
            }
            catch (Exception ex)
            {

                // throw;
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章