如何使用上下文菜单将文件导入我的应用程序(右键单击)

时间:2012-10-13 06:26:48

标签: c# shell stream contextmenu

我想将我的应用程序添加到-right-click-上下文菜单项中,我想在我右键单击文件并选择我的程序时导入文件路径

我正在使用此代码在上下文菜单中添加应用

private void btnAddMenu_Click(object sender, System.EventArgs e)
{
    RegistryKey regmenu = null;
    RegistryKey regcmd = null;
    try
    {
        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(regmenu != null)
            regmenu.Close();
        if(regcmd != null)
            regcmd.Close();
    }        
}

所以任何人都知道如何将文件路径导入应用程序!!

1 个答案:

答案 0 :(得分:1)

这并不困难。基本上,您需要将项目添加到Registry.ClassesRoot.CreateSubKey注册表。

您可以在main(string[] args)方法中阅读参数。它会将其作为args参数的内容传递。您还可以使用Environment.GetCommandLineArgs()方法在应用程序中的任何其他位置获取该值。

查看以下参考资料:

  1. Add a context menu to the Windows Explorer
  2. Add "Open with Notepad" to the Context Menu for All Files