C# 创建系统右键菜单按钮关联指定程序(无需管理员权限)

前言

为了将“解决自媒体一键多平台发布”项目做得更适合自己的使用习惯,Windows端的桌面版也立项了。

本篇文章分享的内容是:实现系统右键菜单按钮关联桌面程序问题。

一、开发环境

  • vs2019
  • Windows 10

二、具体代码

本次实现系统右键菜单按钮功能,是通过Windows注册表实现的。

1、访问HKEY_CURRENT_USER

使用HKEY_CURRENT_USER,是为了避免向用户申请管理员权限。具体代码如下:

//using Microsoft.Win32;
var userRegistry = Registry.CurrentUser;

2、访问Software\Classes\*\shell

当前用户的右键菜单按钮信息(仅指文件),是储存在Software\Classes\*\shell中的,所以我们需要拿到shell的写权限。具体代码如下:

var shellRegistryKey = userRegistry.OpenSubKey("Software").OpenSubKey("Classes").OpenSubKey("*").OpenSubKey("shell", true);

提示:OpenSubKey("shell", true)中的第二个参数设置为true,是为了在shell中写入子项(也就是拿到shell的写权限)。

3、判断准备生成的右键菜单按钮名是否存在

为防止重复写入导致出错,先判断子项名(右键菜单按钮名)是否存在。代码如下:

var isRegistryKeyNameExsit = shellRegistryKey.GetSubKeyNames().Contains(keyName);

4、写入右键菜单按钮名

首先shell中写入子项(可以理解为在注册表中的名字),然后设置刚创建的子项在右键菜单中的按钮名。具体代码:

var contextMenuRegistryKey = shellRegistryKey.CreateSubKey(keyName);

contextMenuRegistryKey.SetValue("", keyName);

提示:contextMenuRegistryKey.SetValue("", keyName)中的keyName是显示在右键菜单中的按钮名,可以不同子项名,使用其他名字。

5、关联指定程序

首先在新建的子项中创建名为command的子项,然后设置子项中的默认项的值为"程序路径" "%1",这样就将指定程序与右键菜单按钮进行了关联。

contextMenuRegistryKey.CreateSubKey("command");
command.SetValue("", $"\"{programPath}\" \"%1\"");

提示:"程序路径" "%1"%1是为了把右键打开关联程序时,把选择的文件路径传给关联程序。

如果关联程序时Winform程序,则还需要修改Winform默认程序,实现接收传入的文件路径的功能。

5.1、修改Winform默认程序

(1)修改Program.cs

将默认的无参Main()方法修改为如下代码:

static void Main(string[] args)

(2)修改Main()方法的默认启动窗口程序

代码如下:

Application.Run(new Form1(args));

(3)修改Form1.cs

为了接收参数,需要一个新的构造函数,具体代码如下:

public Form1(string[] args) : this()
{
    this.label1.Text = string.Join(",", args);
}

提示:this()是为了不修改默认的无参构造函数,直接调用它。

三、完整的程序代码

1、注册表修改

/// <summary>
/// 注册表帮助类
/// 版本:v1
/// 日期:2022年10月15日
/// 作者:hxsfx
/// </summary>
public class RegistryHelper
{
        /// <summary>
        /// 计算机\HKEY_CURRENT_USER\Software\Classes\*\shell
        /// </summary>
        private static RegistryKey RegistryUserSoftwareClassesAllShell
        {
            get
            {
                return Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Classes").OpenSubKey("*").OpenSubKey("shell", true);
            }
        }
        /// <summary>
        /// 生成右键菜单按钮(当前用户)
        /// </summary>
        /// <param name="keyName">右键菜单名</param>
        /// <param name="programPath">点击按钮后打开的程序路径</param>
        public static void CreateUserContextMenu(string keyName, string programPath)
        {
            if (!RegistryUserSoftwareClassesAllShell.GetSubKeyNames().Contains(keyName))
            {
                var contextMenuRegistryKey = RegistryUserSoftwareClassesAllShell.CreateSubKey(keyName);
                contextMenuRegistryKey.SetValue("", keyName);
                var command = contextMenuRegistryKey.CreateSubKey("command");
                command.SetValue("", $"\"{programPath}\" \"%1\"");
            }
        }
        /// <summary>
        /// 删除右键菜单按钮(当前用户)
        /// </summary>
        /// <param name="keyName">右键菜单名</param>
        public static void DeleteUserContextMenu(string keyName)
        {
            if (RegistryUserSoftwareClassesAllShell.GetSubKeyNames().Contains(keyName))
            {
                RegistryUserSoftwareClassesAllShell.DeleteSubKeyTree(keyName);
            }
        }
}

2、Winform代码

(1)Program.cs

static class Program
{
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(args));
    }
}

(1)窗口Form1.cs

public partial class Form1 : Form
{
    public Form1(string[] args) : this()
    {
        this.label1.Text = string.Join(",", args);
    }
    public Form1()
    {
        InitializeComponent();
    }
}

四、最后

其实个人不是很喜欢通过注册表来实现功能,一方面是不了解注册表,另一方面是总觉得注册表不够优雅。哈哈~

如果各位小伙伴有什么其他更好的办法,可以告诉我哦~

posted @ 2022-10-16 09:38  好先生FX  阅读(642)  评论(0编辑  收藏  举报