本文介绍的代码,显示了如何通过操作注册表,添加文件或文件夹右键菜单的方法。
代码如下:
// 添加到注册表 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.jb200.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("反注册成功!", "提示"); }