I am looking for a way to prevent Visual Studio from reflecting project properties changes onto *.csproj files. I want to set properties only for single build action, then they should be discarded.
I am using EnvDTE automation object in my Visual Studio plugin. Below is my example code that sets (or changes) "UseHostCompilerIfAvailable" property to "false" for specific configuration and platform of a project that is about to be built. Build is initiated from Visual Studio UI.
Somewhere in the build initialization process Visual Studio writes all project properties changes to project files, that is what I am trying to prevent.
dte.Events.BuildEvents.OnBuildProjConfigBegin += BuildEvents_OnBuildProjConfigBegin;
...
private void BuildEvents_OnBuildProjConfigBegin(string Project, string ProjectConfig, string Platform, string SolutionConfig)
{
ThreadHelper.ThrowIfNotOnUIThread();
IVsSolution solution = (IVsSolution)this.GetService(typeof(SVsSolution));
solution.GetProjectOfUniqueName(Project, out IVsHierarchy hier);
IVsBuildPropertyStorage storage = hier as IVsBuildPropertyStorage;
storage.SetPropertyValue("UseHostCompilerIfAvailable", $"{ProjectConfig}|{Platform}", (uint)Microsoft.VisualStudio.Shell.Interop._PersistStorageType.PST_PROJECT_FILE, "false");
}
I know that I can save property changes and revert them after build is done or I can cache project files and roll them back in the end, but these approaches have their possible bad side effects. I would be very glad to even get some ideas in which direction I should look at.