【问题标题】:How to fetch the path of current solution opened in visual studio?如何获取在 Visual Studio 中打开的当前解决方案的路径?
【发布时间】:2020-06-10 19:46:26
【问题描述】:

我创建了一个工具窗口作为 VSIX 项目的一部分,并安装了一个扩展来测试它。因此,工具窗口需要访问在 Visual Studio 中打开的当前解决方案/项目。当用户第一次创建项目时,我们可以使用如下所述的 DTE 对象获取路径,我尝试了它们,它工作正常,

How can an add-in detect when a solution is loaded?

Acessing currently opened solution in a vsix project

但是下次当用户双击解决方案时,如何获取当前的解决方案路径?现在当用户打开 .sln 文件时,工具窗口会因为找不到打开的解决方案路径而中断。

【问题讨论】:

    标签: visual-studio-2019 visual-studio-extensions vsix project-template customtool


    【解决方案1】:

    当用户双击并打开解决方案时,我们手头没有DTE 对象,我们需要一种不同的方式来获取解决方案信息。我使用IVsSolution.GetSolutionInfo 使其工作

    IVsSolution solution = (IVsSolution) Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(IVsSolution));
    solution.GetSolutionInfo(out string solutionDirectory, out string solutionName, out string solutionDirectory2);
    var solutionPath = solutionDirectory + System.IO.Path.GetFileNameWithoutExtension(solutionName);
    

    【讨论】:

      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多