【问题标题】:Visual Studio Extension: Get the path of the current selected file in the Solution ExplorerVisual Studio 扩展:在解决方案资源管理器中获取当前选定文件的路径
【发布时间】:2016-04-13 03:36:27
【问题描述】:

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

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

正如 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;
            }

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

【问题讨论】:

    标签: visual-studio-extensions vsx vspackage


    【解决方案1】:

    供将来参考:

    //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);
            
         }
    }
    

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

    【讨论】:

      【解决方案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方法来查询当前选定的项目。

      【讨论】:

      • 你是对的。几天后我才注意到这一点。我一直在业余时间这样做,所以我完全忘记了我什至用该值设置了一个类变量。我正在编辑问题并添加另一个可能的解决方案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 2013-06-09
      相关资源
      最近更新 更多