// 添加到注册表private void btnRegister_Click(object sender, EventArgs e){ if (this.tbMenuTitle.Text.Length == 0) return; // 注册到文件 if (this.ckRegToFile.Checked) { RegistryKey shell = Registry.ClassesRoot.OpenSubKey("*", true).OpenSubKey("shell", true); if (shell == null) shell = Registry.ClassesRoot.OpenSubKey("*", true).CreateSubKey("shell"); RegistryKey custome = shell.CreateSubKey(this.tbMenuTitle.Text); RegistryKey cmd = custome.CreateSubKey("command"); cmd.SetValue("", Application.ExecutablePath + " %1"); cmd.Close(); custome.Close(); shell.Close(); } // 注册到文件夹 if (this.ckRegToDir.Checked) { RegistryKey shell = Registry.ClassesRoot.OpenSubKey("directory", true).OpenSubKey("shell", true); if (shell == null) shell = Registry.ClassesRoot.OpenSubKey("directory", true).CreateSubKey("shell"); RegistryKey custome = shell.CreateSubKey(this.tbMenuTitle.Text); RegistryKey cmd = custome.CreateSubKey("command"); cmd.SetValue("", Application.ExecutablePath + " %1"); cmd.Close(); custome.Close(); shell.Close(); } MessageBox.Show("注册成功!", "提示");}// 反注册private void btnUnRegister_Click(object sender, EventArgs e){ RegistryKey shell = Registry.ClassesRoot.OpenSubKey("*", true).OpenSubKey("shell", true); if (shell != null) shell.DeleteSubKeyTree(this.tbMenuTitle.Text); shell = Registry.ClassesRoot.OpenSubKey("directory", true).OpenSubKey("shell", true); if (shell != null) shell.DeleteSubKeyTree(this.tbMenuTitle.Text); shell.Close(); MessageBox.Show("反注册成功!", "提示");}
C# 注册自己的右键菜单 (文件夹/文件)
最新推荐文章于 2024-04-18 18:57:38 发布
本文介绍了一种使用C#实现的方法,该方法能够将应用程序注册为Windows资源管理器上下文菜单的一项,并提供了反注册的功能。通过创建注册表项,可以在文件或文件夹上右击时显示自定义的菜单选项。
674

被折叠的 条评论
为什么被折叠?



