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.