如何获取当前的 sln 路径?
您可以通过 Dte.Solution.FullName 方法获取当前 sln,如下所示:
DTE2 dte = (DTE2)this.ServiceProvider.GetService(typeof(DTE));
ConfigurationManager configmgr;
Configuration config;
var solution = dte.Solution;
if (dte.Solution.Projects.Count > 0)
{
string solutionPath = solution.FullName;
}
为选定项目生成构建工件的任何替代方法?
请参考以下代码,获取特殊项目并构建它。
请添加参考:使用 Microsoft.Build.Construction;
使用 Microsoft.Build.Evaluation;
使用 Microsoft.Build.Framework;
string solutionPath = "yousolutionPath.sln";
var solutionFile = SolutionFile.Parse(solutionPath);
foreach (var item in solutionFile.ProjectsInOrder)
{
Project project = ProjectCollection.GlobalProjectCollection.LoadProject(item.AbsolutePath);
project.SetGlobalProperty("Configuration", "Debug");
if (project.GetPropertyValue("RootNamespace") == "CppApp5")
{
project.Build("Build");
}
}
正如 Jason 所说,您可以通过以下代码获取 currentSoution。
var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();
Microsoft.CodeAnalysis.Solution sln = workspace.CurrentSolution;