C# 如何在 Windows&nbsp…
2014-06-07 17:46
477 查看
方法:通过改写注册表实现
一、给所有类型的文件添加自定义的右键菜单
HKEY_CLASSES_ROOT\*\shell
HKEY_CLASSES_ROOT\*\shell\自定义的菜单名
HKEY_CLASSES_ROOT\*\shell\自定义的菜单名\command
值名称:(默认)
类型:REG_SZ
数据:关联程序的完全限定名称
二、给所有文件夹添加自定义的右键菜单
HKEY_CLASSES_ROOT\Directory\shell
HKEY_CLASSES_ROOT\Directory\shell\自定义的菜单名
HKEY_CLASSES_ROOT\Directory\shell\自定义的菜单名\command
值名称:(默认)
类型:REG_SZ
数据:关联程序的完全限定名称
实现:C# WinForm
using Microsoft.Win32;
private void AddFileContextMenuItem(string itemName, string
associatedProgramFullPath)
{
//创建项:shell
RegistryKey
shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell",true);
if (shellKey ==
null)
{
shellKey =
Registry.ClassesRoot.CreateSubKey(@"*\shell");
}
//创建项:右键显示的菜单名称
RegistryKey
rightCommondKey = shellKey.CreateSubKey(itemName);
RegistryKey
associatedProgramKey=
rightCommondKey.CreateSubKey("command");
//创建默认值:关联的程序
associatedProgramKey.SetValue(string.Empty,
associatedProgramFullPath);
//刷新到磁盘并释放资源
associatedProgramKey.Close();
rightCommondKey.Close();
shellKey.Close();
}
private void AddDirectoryContextMenuItem(string itemName,
string associatedProgramFullPath)
{
//创建项:shell
RegistryKey
shellKey = Registry.ClassesRoot.OpenSubKey(@"directory\shell",
true);
if (shellKey ==
null)
{
shellKey =
Registry.ClassesRoot.CreateSubKey(@"*\shell");
}
//创建项:右键显示的菜单名称
RegistryKey
rightCommondKey = shellKey.CreateSubKey(itemName);
RegistryKey
associatedProgramKey =
rightCommondKey.CreateSubKey("command");
//创建默认值:关联的程序
associatedProgramKey.SetValue("",
associatedProgramFullPath);
//刷新到磁盘并释放资源
associatedProgramKey.Close();
rightCommondKey.Close();
shellKey.Close();
}
Reference: http://blog.sina.com.cn/s/blog_752 a806
ca76a0100y5yv.html
一、给所有类型的文件添加自定义的右键菜单
HKEY_CLASSES_ROOT\*\shell
HKEY_CLASSES_ROOT\*\shell\自定义的菜单名
HKEY_CLASSES_ROOT\*\shell\自定义的菜单名\command
值名称:(默认)
类型:REG_SZ
数据:关联程序的完全限定名称
二、给所有文件夹添加自定义的右键菜单
HKEY_CLASSES_ROOT\Directory\shell
HKEY_CLASSES_ROOT\Directory\shell\自定义的菜单名
HKEY_CLASSES_ROOT\Directory\shell\自定义的菜单名\command
值名称:(默认)
类型:REG_SZ
数据:关联程序的完全限定名称
实现:C# WinForm
using Microsoft.Win32;
private void AddFileContextMenuItem(string itemName, string
associatedProgramFullPath)
{
//创建项:shell
RegistryKey
shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell",true);
if (shellKey ==
null)
{
shellKey =
Registry.ClassesRoot.CreateSubKey(@"*\shell");
}
//创建项:右键显示的菜单名称
RegistryKey
rightCommondKey = shellKey.CreateSubKey(itemName);
RegistryKey
associatedProgramKey=
rightCommondKey.CreateSubKey("command");
//创建默认值:关联的程序
associatedProgramKey.SetValue(string.Empty,
associatedProgramFullPath);
//刷新到磁盘并释放资源
associatedProgramKey.Close();
rightCommondKey.Close();
shellKey.Close();
}
private void AddDirectoryContextMenuItem(string itemName,
string associatedProgramFullPath)
{
//创建项:shell
RegistryKey
shellKey = Registry.ClassesRoot.OpenSubKey(@"directory\shell",
true);
if (shellKey ==
null)
{
shellKey =
Registry.ClassesRoot.CreateSubKey(@"*\shell");
}
//创建项:右键显示的菜单名称
RegistryKey
rightCommondKey = shellKey.CreateSubKey(itemName);
RegistryKey
associatedProgramKey =
rightCommondKey.CreateSubKey("command");
//创建默认值:关联的程序
associatedProgramKey.SetValue("",
associatedProgramFullPath);
//刷新到磁盘并释放资源
associatedProgramKey.Close();
rightCommondKey.Close();
shellKey.Close();
}
Reference: http://blog.sina.com.cn/s/blog_752 a806
ca76a0100y5yv.html
相关文章推荐
- windows 7 如何关闭 程序崩溃时弹出的"windows正在检查该问题的解决方案"
- 如何设置Windows server 2008的IE…
- 如何使用 Pie controls功能
- C# .net的winform开发中如何…
- 如何使用 Pie controls功能
- C# Winform中如何实现窗体之…
- 如何在Windows 7中还原.dll文件的…
- 如何使用 Pie controls功能
- 如何使用 Pie controls功能
- C# .net的winform开发中如何…
- C# 如何获取读取项目资源中的数据&…
- 如何使用 Pie controls功能
- 如何: 如何禁用在 MFC&…
- 如何使用 Pie controls功能
- <c#>windows 消息 16进制编码
- 如何在&nbsp;LoadRunner&nbsp;脚本中做关联&nbsp;(…
- Use Windows Authentication in ASP.NET 2.0/ASP.NET 2.0 中的 Windows 身份验证
- C#&nbsp;Windows&nbsp;form&nbsp;学习积累《一》
- 如何使用 Pie controls功能
- Use Windows Authentication in ASP.NET 2.0/ASP.NET 2.0 中的 Windows 身份验证