[转]C# 注册自己的右键菜单 (文件夹/文件)
2011-05-26 17:47
183 查看
一个小小的例子, 演示了如何通过操作注册表, 添加自己的文件或文件夹右键菜单.
// 添加到注册表 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("反注册成功!", "提示"); } 另外一个例子codeproject上的: http://www.codeproject.com/KB/cs/appendmenu.aspx?msg=3335190#xx3335190xx
相关文章推荐
- C#、VB.NET将自己的程序注册到系统的右键菜单(所有文件和目录)
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列 - ContextMenu 注册文件右键菜单
- 改注册表实现的增加右键菜单右键菜单,注册自己的文件格式
- (C#)Windows Shell 外壳编程系列 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
- 添加IE右键菜单 以 调用和运行 自己的程序或文件
- 为文件、文件夹添加复制路径的右键菜单
- Windows Explorer中对所选文件增加右键菜单并关联自己程序的例子
- UltraEdit 32 的右键菜单注册文件
- VS code 编辑器(文件或者文件夹的右键打开菜单的显示)
- 为快捷方式文件(lnk文件)右键菜单添加“打开所在文件夹”
- 自定义鼠标右键(层叠式菜单:cascading menu)(文件系统右键、文件夹系统右键和桌面鼠标右键)
- 【转】如何给文件夹注册右键菜单
- 右键菜单复制文件/文件夹路径到剪贴板
- VSCode 绿色版(zip压缩包) 添加右键菜单 使用VSCode 打开文件或文件夹