教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 C# 注册右键菜单、文件夹与文件的实现代码

C# 注册右键菜单、文件夹与文件的实现代码

发布时间:2016-03-17   编辑:jiaochengji.com
本文介绍下C#中注册右键菜单、注册文件夹、注册文件的实现代码,有需要的朋友,可以参考下。

本文介绍的代码,显示了如何通过操作注册表,添加文件或文件夹右键菜单的方法。

代码如下:

// 添加到注册表
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();
}

// 注册到文件夹 www.jbxue.com
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# 注册右键菜单、文件夹与文件的实现代码
C#.NET程序添加到右键菜单的实现代码
XP系统减少开机滚动条时间的方法
C#自定义控件添加右键菜单的实现代码
Windows 7右键弹出菜单没有新建文件夹按钮怎么办
“我的电脑”、“注册表”等无法打开问题解决方法
C# winform treeview添加右键菜单并选中节点的方法
VBS 添加右键以计算文件MD5值的代码(图文)
c# TreeView添加右键快键菜单的二个方法
解决IE/360浏览器无法查看源文件的办法总结

[关闭]
~ ~