我在VSIX项目中创建了一个工具窗口,并安装了扩展程序对其进行测试。因此,工具窗口需要访问在Visual Studio中打开的当前解决方案/项目。当用户首次创建项目时,我们可以使用下面提到的DTE对象获取路径,我尝试了一下,效果很好,
How can an add-in detect when a solution is loaded?
Acessing currently opened solution in a vsix project
但是当用户双击解决方案时,下次如何获取当前的解决方案路径?现在,当用户打开.sln文件时,由于无法找到打开的解决方案路径,工具窗口中断。
[当用户双击并打开解决方案时,我们手头没有“ DTE”对象,我们需要一种不同的方式来获取solutionInfo。我使用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);