题
林不知道确切的术语,我应该叫什么。我想快捷方式添加到我的C#程序,当我在窗口点击右键。
这是我的发现,它得到的东西做配置“注册表编辑器”。我有这样的例子,但它是为IE做。任何人都可以点我的任何引用,可以解决我的问题呢?
的引用:
http://blog.voidnish.com/?p=17 的 http://www.codeguru.com/ CPP /杂项/杂项/的InternetExplorer / article.php / c11007 / 的
非常感谢你。
<强>今天更新.. 强>
根据从因子神秘响应,我这个代码添加到原始。我有2个解决方案。一,它在注册表中创建的 HKEY_ CLASSES_ ROOT ,但我看不到结果,当我右键单击文档文件。
private const string ProgName = "Software\\Classes\\Word.Document\\shell";
private const string MenuName = "Software\\Classes\\Word.Document\\shell\\NewTesting";
public const string Command =Software\\Classes\\Word.Document\\shell\\NewTesting\\command";
private void Form1_Load(object sender, EventArgs e)
{
txtProgram.Text = "Word.Document.8";
txtName.Text = "Testing";
txtPath.Text = "C:\\temp\\encriptTest.exe";
check();
addItem()
}
public void check()
{
RegistryKey regmenu = null;
RegistryKey regcmd = null;
try
{
//this.CheckSecurity();
regmenu = Registry.ClassesRoot.OpenSubKey(MenuName, false);
}
catch (ArgumentException ex)
{
// RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList.
MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess. "
+ "AllAccess cannot be used as a parameter in GetPathList because it represents more than one "
+ "type of registry variable access : \n" + ex);
}
catch (SecurityException ex)
{
// RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList.
MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess. " + ex);
this.btnAddMenu.Enabled = false;
//this.btnRemoveMenu.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
}
}
private void CheckSecurity()
{
//check registry permissions
RegistryPermission regPerm;
regPerm = new RegistryPermission(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + ProgName);
regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + MenuName);
regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + Command);
regPerm.Demand();
}
private void addItem()
{
RegistryKey regmenu = null;
RegistryKey regcmd = null;
RegistryKey regprog = null;
try
{
regprog = Registry.ClassesRoot.CreateSubKey(ProgName);
if (regmenu != null)
regmenu.SetValue("", this.txtProgram.Text);
regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
if (regmenu != null)
regmenu.SetValue("", this.txtName.Text);
regcmd = Registry.ClassesRoot.CreateSubKey(Command);
if (regcmd != null)
regcmd.SetValue("", this.txtPath.Text);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regprog != null)
regprog.Close();
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
}
}
<强>其次,创建在HKEY_ LOCAL_ MACHINE。强>
private bool Add_Item(string Extension,string MenuName, string MenuDescription, string MenuCommand)
{
//receive .doc,OpenTest,Open with Opentest,path: C:\\temp\\encriptTest.exe %1
bool ret = false;
RegistryKey rkey = //receive .doc extension (word.Document.8)
Registry.ClassesRoot.OpenSubKey(Extension); //set HKEY_LOCAL_MACHINE\software\classes\word.Document.8
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) //with extension file receive OpenTest as shell
{
string strkey = "shell\\" + MenuName + "\\command"; //..\shell\OpenTest\command
RegistryKey subky = rkey.CreateSubKey(strkey);
if (subky != null)
{
subky.SetValue("", MenuCommand); // path: C:\\temp\\encriptTest.exe %1
subky.Close();
subky = rkey.OpenSubKey("shell\\" + MenuName, true); //..\shell\OpenTest
if (subky != null)
{
subky.SetValue("", MenuDescription); // name displayed: Open with &OpenTest
subky.Close();
}
ret = true;
}
rkey.Close();
}
}
}
}
return ret;
}
}
我的关注,主要的关键,我应该使用哪一个?
解决方案 3
非常感谢您的答复。 Very2 apreciate他们..
按Conlcusion,解决我的概率3种方式。在容易understadable方法:
添加快捷在3种方式:
<强> 1。直接在注册表窗口中创建:强> http://www.codeguru.com/cpp/misc /misc/internetexplorer/article.php/c11007/
<强> 2。快捷方式只提供给文件夹。强> http://www.codeproject.com/KB/cs/appendmenu.aspx http://blog.voidnish.com/?p=17
第3。快捷方式提供给所有文件和文件夹。 http://www.autoitscript.com/forum/index。 PHP?showtopic = 103265&查看= findpost&p = 731920个
其他提示
我相信你想将项目添加到右键菜单。 下面是在CodeProject一个很好的文章,告诉您如何做到这一点: http://www.codeproject.com/KB/cs/appendmenu.aspx(基本上它只是加入适当的键来Windows注册表)
您会想确定的.doc文件的文件类型(进程id)。你可以找到这个在HKEY_CURRENT_USER\Software\Classes\.doc
(它是默认值)。
然后添加键HKEY_CURRENT_USER\Software\Classes\<ProgID>\shell\NewMenuOption\command
,其中所述默认值是路径到你的程序。
您可以做到这一切与Registry.SetValue
和GetValue
。
编辑:附加信息,配置单元密钥之间的差:
和HKEY_LOCAL_MACHINE\Software\Classes
HKEY_CURRENT_USER\Software\Classes
相似,但HKLM为系统默认/所有用户设置,并HKCU为每个用户设置。按照用户设置不需要提升权限,所以你可以写你的上下文菜单键作为具有无痛苦普通用户。
HKEY_CLASSES_ROOT
是结合HKEY_LOCAL_MACHINE\Software\Classes
和HKEY_CURRENT_USER\Software\Classes
的图,与针对HKLM写入。这是一个快捷方式书写系统默认值,很多教程表明这一点,因为它的略的简单,但除非你是为所有用户安装应用程序,我不推荐它。
上MSDN 高级注册表信息