Search code examples
c#visual-studiovisual-studio-extensionsenvdte

Visual Studio extension: Change the hint path of an assembly reference


I'm writing a Visual Studio extension and I would like to change the hint path of an assembly reference of a C#-project without to trigger the "File Modification Detected"-dialog.

<Reference Include="SomeAssembly">
  <HintPath>C:\ChangeMe\SomeAssembly.dll</HintPath>
</Reference>

But in the VSLangProj110.Reference5-interface I can't find any property that I can use. (Accessed through VSLangProj140.VSProject3.References)


Solution

  • Microsoft.Build.BuildEngine.Project is outdated. Here is an updated working solution.

    foreach (var dteProject in dte.Solution.Projects.OfType<Project>())
    {
        // You can edit the project through an object of Microsoft.Build.Evaluation.Project 
        var buildProject = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(dteProject.FullName).First();
        foreach (var item in buildProject.Items.Where(obj => obj.ItemType == "Reference"))
        {
            var newPath = SomeMethod(item.GetMetadata("HintPath"));
    
            item.SetMetadataValue("HintPath", newPath);
        }
    
        // But you have to save through an object of EnvDTE.Project
        dteProject.Save();
    }