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

2019-05-06 16:25发布

问题:

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.

回答1:

Use the EnvDTE.Document interface:

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

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



回答2:

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.