如何将项目添加到窗口的上下文菜单中[仅适用于pdf文件和doc文件]

时间:2015-09-25 10:24:16

标签: c# .net windows winforms

我为虚拟打印机创建了一个c#应用程序,但现在我正在寻找启动我的应用程序,同时右键单击任何.pdf文件或任何.doc文件

简而言之,我想在窗口的上下文菜单中添加项目,但仅限于.pdf文件和.doc文件。

请建议我如何实现它。

提前感谢。

1 个答案:

答案 0 :(得分:2)

要知道要修改/添加的键,请在此处查看接受的答案: Add menu item to windows context menu only for specific filetype

要使用C#添加密钥,请使用 RegistryKey 对象

string[] exts = {".pdf", ".doc"};
foreach (string ext in exts)
{
    RegistryKey _key = Registry.ClassesRoot.OpenSubKey($"HKEY_CLASSES_ROOT\\{ext}\\shell", true);
    RegistryKey newkey = _key.CreateSubKey("Use Virtual Printer");

    RegistryKey subNewkey = newkey.CreateSubKey("Command");
    subNewkey.SetValue("", "C:\\yourApplication.exe");
    subNewkey.Close();

    newkey.Close();
    _key.Close();
} 

How add context menu item to Windows Explorer for folders修改

相关问题