开发者

VB.NET EnvDTE Up-To-Date check before building project

how do I check if a project is up-to-date?

I'm basically trying to programmatically build each project in a list but only if they have changed. So does anyone know of a way (using EnvDTE maybe) to check if a project changed and therefore needs to compile开发者_运维知识库?

Thanks in advance for all the help.


Use the EnvDTE.Document interface:

Document d = //get document
bool saved = d.Saved;

The Saved property will be false if the document is dirty.


You can do it using the UpToDate property of VCConfiguration.

Assuming you can get an instance of EnvDTE.DTE you can get the required VCProjectEngine VCConfiguration for the active project like this:

public Project GetSolutionStartupProject(DTE dte)
{
  string startupProjectName = String.Empty;

  SolutionBuild solutionBuild = dte.Solution.SolutionBuild as SolutionBuild;
  foreach (string item in solutionBuild.StartupProjects as Array)
  {
    startupProjectName += item;
  }

  return dte.Solution.Item(startupProjectName);
}

public void BuildStartupProject(DTE dte)
{
  Project project = GetSolutionStartupProject(dte);
  Configuration activeConfiguration = project.ConfigurationManager.ActiveConfiguration;

  // annoyingly can't just do activeConfiguration.Object as VCConfiguration
  //
  VCProject vcProject = project.Object as VCProject;
  VCConfiguration vcActiveConfiguration = null;
  foreach (VCConfiguration vcConfiguration in vcProject.Configurations)
  {
    if (vcConfiguration.ConfigurationName == activeConfiguration.ConfigurationName &&
        vcConfiguration.Platform.Name == activeConfiguration.PlatformName)
    {
      vcActiveConfiguration= vcConfiguration;
      break;
    }
  }

  if (!vcActiveConfiguration.UpToDate)
  {
    vcActiveConfiguration.Build();
  }
}

If you wanted to do all projects in the solution then it would be easier as you don't need to find the startup project. The important part is converting an instance of DTE Project to a VCProjectEngine VCProject, looping the VCConfigurations once you've done that should be easy.

Also worth noting that VCConfiguration.Build() will only build that configuration and fail if the dependencies haven't been built. You can use SolutionBuild.BuildProject() to build the project and its dependencies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜