如何获取上次构建的输出目录?

8

假设我有一个包含一个或多个项目的解决方案,并且我刚刚使用以下方法启动了构建:

_dte.Solution.SolutionBuild.Build(true); // EnvDTE.DTE

我怎样才能获取每个刚刚构建的项目的输出路径?例如...

c:\MySolution\Project1\Bin\x86\Release\
c:\MySolution\Project2\Bin\Debug


类似问题:https://dev59.com/uFXTa4cB1Zd3GeqP4Lj9 - Omer Raviv
2个回答

11
请不要告诉我这是唯一的方法...
// dte is my wrapper; dte.Dte is EnvDte.DTE               
var ctxs = dte.Dte.Solution.SolutionBuild.ActiveConfiguration
              .SolutionContexts.OfType<SolutionContext>()
              .Where(x => x.ShouldBuild == true);
var temp = new List<string>(); // output filenames
// oh shi
foreach (var ctx in ctxs)
{
    // sorry, you'll have to OfType<Project>() on Projects (dte is my wrapper)
    // find my Project from the build context based on its name.  Vomit.
    var project = dte.Projects.First(x => x.FullName.EndsWith(ctx.ProjectName));
    // Combine the project's path (FullName == path???) with the 
    // OutputPath of the active configuration of that project
    var dir = System.IO.Path.Combine(
                        project.FullName,
                        project.ConfigurationManager.ActiveConfiguration
                        .Properties.Item("OutputPath").Value.ToString());
    // and combine it with the OutputFilename to get the assembly
    // or skip this and grab all files in the output directory
    var filename = System.IO.Path.Combine(
                        dir,
                        project.ConfigurationManager.ActiveConfiguration
                        .Properties.Item("OutputFilename").Value.ToString());
    temp.Add(filename);
}

这让我想要呕吐。


我想说至少有一个"FullOutputPath"。哦,如果想要获取最后一次成功的构建,你需要检查SolutionBuild.LastBuildInfo,它只显示失败构建的计数。 - Terrance
@Terrance:嗨。已经在检查LBI了,但据我所知没有FullOutputPath。我可以获取Project.Properties.Item("FullPath")并将其与ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath")组合使用。 - user1228
4
我确定这对你来说是古老历史了,但属性“OutputFileName”似乎没有与配置相关联,而是与项目本身相关联(这很有道理,因为它不会在配置之间更改)。 但是,为了让我在VS2015中使其工作,我必须使用project.Properties.Item("OutputFileName").Value.ToString() - Kirk Woll
找了好久,终于找到了这个帮助我解决问题的方法。有一个改进建议,不要使用'ToString()',而是建议始终使用"(string)..",否则如果'Value'返回'null',你将会得到一个异常。 - AndresRohrAtlasInformatik

6
您可以通过遍历EnvDTE中每个项目的“已构建”输出组中的文件名来访问输出文件夹。请注意保留HTML代码中的标签。
var outputFolders = new HashSet<string>();
var builtGroup = project.ConfigurationManager.ActiveConfiguration.OutputGroups.OfType <EnvDTE.OutputGroup>().First(x => x.CanonicalName == "Built");

foreach (var strUri in ((object[])builtGroup.FileURLs).OfType<string>())
{
  var uri = new Uri(strUri, UriKind.Absolute);
  var filePath = uri.LocalPath;
  var folderPath = Path.GetDirectoryName(filePath);
  outputFolders.Add(folderPath.ToLower());
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接