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

Eux*_*Eux 5 vsx vspackage visual-studio-extensions

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

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

正如马茨所说,答案就在我发布的链接中。只是我写的时候没注意到而已。同时,我还在该线程中找到了另一个可能的答案: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;
            }
Run Code Online (Sandbox Code Playgroud)

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

小智 6

备查:

//In your async method load the DTE
var dte2 = await ServiceProvider.GetGlobalServiceAsync(typeof(SDTE)) as DTE2;
var selectedItems = dte2.SelectItems;
if(selectedItems.MultiSelect || selectedItems.Count > 1){ //Use either/or
     for(short i = 1; i <= selectedItems.Count; i++){
        //Get selected item
        var selectedItem = selectedItems[i];
        //Get associated project item (selectedItem.ProjectItem  
        //If selectedItem is a project, then selectedItem.ProjectItem will be null,
        //and selectedItem.Project will not be null.
        var projectItem = selectedItem.ProjectItem;
        //Get project for ProjectItem
        var project = projectItem.ContainingProject;
        // Or get project object if selectedItem is a project
        var sproject = selectedItem.Project;
        //Is selectedItem a physical folder?
        var isFolder = projectItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder;
        //Else, get item's folder
        var itemFolder = new FileInfo(projectItem.Properties.Item("FullPath").ToString()).Directory;
        //Find config file
        var configFiles itemFolder.GetFiles("web.config");
        var configfile = configFiles.length > 0 ? configFiles[0] : null;
        //Turn config file into ProjectItem object
        var configItem = dte2.solution.FindProjectItem(configFile.FullName);
        
     }
}
Run Code Online (Sandbox Code Playgroud)

我希望有人觉得这有帮助......


Mat*_*tze 3

您提到的文章已经包含了该问题的解决方案。

\n\n

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

\n\n
IVsHierarchy hierarchy = null;\nuint itemid = VSConstants.VSITEMID_NIL;\n\nif (IsSingleProjectItemSelection(out hierarchy, out itemid))\n{\n    IVsProject project;\n    if ((project = hierarchy as IVsProject) != null)\n    {\n        string itemFullPath = null;\n        project.GetMkDocument(itemid, out itemFullPath);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n