Visual Studio 扩展:在解决方案资源管理器中获取当前选定文件的路径 [英] Visual Studio Extension: Get the path of the current selected file in the Solution Explorer

查看:70
本文介绍了Visual Studio 扩展:在解决方案资源管理器中获取当前选定文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Visual Studio 创建我的第一个扩展,到目前为止我一直在关注本教程以帮助我入门(http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-视觉工作室扩展很难).现在,当我单击解决方案资源管理器中的文件时,会出现一个自定义菜单项.我现在需要为我的小项目获取在解决方案资源管理器中选择的文件的路径,但我不明白我该怎么做.有什么帮助吗?

I'm trying to create my first extension for visual studio and so far I've been following this tutorial to get me started (http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-visual-studio-extensions-was-hard). Now I have a custom menu item appearing when I click on a file in the solution explorer. What I need now for my small project is to get the path of the file selected in the solution explorer but I can't understand how can I do that. Any help?

---------------------------- 编辑 ------------------------------

---------------------------- EDIT ------------------------------

正如 matze 所说,答案在我发布的链接中.只是写的时候没注意.与此同时,我还在这个帖子中找到了另一个可能的答案:如何使用 vs package 在解决方案资源管理器中获取所选项目的详细信息

As matze said, the answer is in the link I posted. I just didn't notice it when I wrote it. In the meanwhile I also found another possible answer in this thread: How to get the details of the selected item in solution explorer using vs package

我在哪里找到此代码:

foreach (UIHierarchyItem selItem in selectedItems)
            {
                ProjectItem prjItem = selItem.Object as ProjectItem;
                string filePath = prjItem.Properties.Item("FullPath").Value.ToString();
                //System.Windows.Forms.MessageBox.Show(selItem.Name + filePath);
                return filePath;
            }

因此,这里有两种方法可以获取所选文件的路径:)

So, here are two ways to get the path to the selected file(s) :)

推荐答案

你提到的文章已经包含了解决方案.

The article you mentioned already contains a solution for that.

在示例代码中查找 menuCommand_BeforeQueryStatus 方法.它使用 IsSingleProjectItemSelection 方法来获取表示项目的 IVsHierarchy 对象以及所选项目的 id.似乎您可以安全地将层次结构转换为 IVsProject 并使用它的 GetMkDocument 函数来查询项目的完整路径...

Look for the menuCommand_BeforeQueryStatus method in the sample code. It uses the IsSingleProjectItemSelection method to obtain an IVsHierarchy object representing the project as well as the id of the selected item. It seems that you can safely cast the hierarchy to IVsProject and use it´s GetMkDocument function to query the item´s fullpath...

IVsHierarchy hierarchy = null;
uint itemid = VSConstants.VSITEMID_NIL;

if (IsSingleProjectItemSelection(out hierarchy, out itemid))
{
    IVsProject project;
    if ((project = hierarchy as IVsProject) != null)
    {
        string itemFullPath = null;
        project.GetMkDocument(itemid, out itemFullPath);
    }
}

我不想将文章中的整个代码复制到这个答案中,但是 IsSingleProjectItemSelection 函数如何获取所选项目可能很有趣;所以我只是添加了一些注释,它们可能会引导到正确的方向......该方法使用 GetCurrentSelection 全局IVsMonitorSelection 服务的方法,用于查询当前选中的项目.

I don´t want to copy the entire code from the article into this answer, but it might be of interest how the IsSingleProjectItemSelection function obtains the selected item; so I just add some notes instead which may guide into the right direction... The method uses the GetCurrentSelection method of the global IVsMonitorSelection service to query to the current selected item.

这篇关于Visual Studio 扩展:在解决方案资源管理器中获取当前选定文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆