如何从上一次构建中获取输出目录?

7 vsx build envdte

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

_dte.Solution.SolutionBuild.Build(true); // EnvDTE.DTE
Run Code Online (Sandbox Code Playgroud)

如何获取刚刚构建的每个项目的输出路径?例如...

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

小智 10

请不要告诉我这是唯一的方法......

// 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);
}
Run Code Online (Sandbox Code Playgroud)

这让我想要呕吐.

  • 我相信这对你来说是古老的历史,但属性`"OutputFileName"`似乎并没有附加到配置上,而是附加到项目本身(这是有意义的,因为它不会在配置之间改变).但是为了让我在VS2015中工作,我不得不使用`project.Properties.Item("OutputFileName").Value.ToString()`. (4认同)

vas*_*lvk 6

您可以通过遍历BuiltEnvDTE中每个项目的输出组中的文件名来访问输出文件夹:

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());
}
Run Code Online (Sandbox Code Playgroud)