本文介绍了如何通过单击文件从上下文菜单接收命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是在指定文件的上下文菜单中创建项目的func。
如何获取被单击的文件路径?谢谢。
Here is the func that create item in context menu of specified file.
How to get path of file, which was clicked? Thanks.
using Microsoft.Win32;
//Extension - Extension of the file (.zip, .txt etc.)
//MenuName - Name for the menu item (Play, Open etc.)
//MenuDescription - The actual text that will be shown
//MenuCommand - Path to executable
private bool AddContextMenuItem(string Extension,
string MenuName, string MenuDescription, string MenuCommand)
{
bool ret = false;
RegistryKey rkey =
Registry.ClassesRoot.OpenSubKey(Extension);
if(rkey != null)
{
string extstring = rkey.GetValue("").ToString();
rkey.Close();
if( extstring != null )
{
if( extstring.Length > 0 )
{
rkey = Registry.ClassesRoot.OpenSubKey(
extstring,true);
if(rkey != null)
{
string strkey = "shell\\" + MenuName + "\\command";
RegistryKey subky = rkey.CreateSubKey(strkey);
if(subky != null)
{
subky.SetValue("",MenuCommand);
subky.Close();
subky = rkey.OpenSubKey("shell\\" +
MenuName, true);
if(subky != null)
{
subky.SetValue("",MenuDescription);
subky.Close();
}
ret = true;
}
rkey.Close();
}
}
}
}
return ret;
}
推荐答案
if(subky != null)
{
subky.SetValue("",MenuCommand + " \"%1\");
...
}
%1 会给你选择的文件名
The "%1" will give you the filename which was selected
这篇关于如何通过单击文件从上下文菜单接收命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!