首页 文章

Visual Studio扩展:在解决方案资源管理器中获取当前所选文件的路径

提问于
浏览
3

我_2559291已经按照本教程开始(http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-visual-studio-extensions-was-hard) . 现在,当我单击解决方案资源管理器中的文件时,我出现了一个自定义菜单项 . 我现在需要的小项目是获取在解决方案资源管理器中选择的文件的路径,但我无法理解我该怎么做 . 有帮助吗?

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

正如matze所说,答案就在我发布的链接中 . 当我写这篇文章时,我才注意到它 . 与此同时,我也在这个帖子中找到了另一个可能的答案: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;
            }

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

1 回答

  • 2

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

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

    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 函数如何获取所选项目可能会很有意义;所以我只是添加一些注释,这可能会引导到正确的方向......该方法使用全局 IVsMonitorSelection 服务的GetCurrentSelection方法查询当前所选项目 .

相关问题