1

I need to force all of my projects to use dll's that are stored in a common location. I am starting by refactoring existing projects to point to the dll's source folder of my preference. So I decided to try a programmatic way for changing these references.

I read dll references as following:

 XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
            XDocument projDefinition = XDocument.Load(filePath);
            IEnumerable<string> references = projDefinition
                .Element(msbuild + "Project")
                .Elements(msbuild + "ItemGroup")
                .Elements(msbuild + "Reference")
                .Elements(msbuild + "HintPath")
                .Select(refElem => refElem.Value);

The next step is to change reference HintPath based on reference name. I plan on creating a dictionary of references and then run through them to update each of the project files.

However, I cannot find any solid way of reading and changing HintPath based on Reference inside csproj.

leppie
  • 115,091
  • 17
  • 196
  • 297
eYe
  • 1,695
  • 2
  • 29
  • 54
  • Check out this answer to a similar question: http://stackoverflow.com/a/35233082/33082 – Michael G Apr 12 '16 at 14:19
  • The answer shows exactly what I do, plus it dumps the reference that was read to a txt. Not very useful. What I need is being able to modify HintPath based on the Reference. – eYe Apr 12 '16 at 14:32

1 Answers1

0

I am likely quite late to this and i am yet to test out this particular application but you should be able to take the code you have and remove the .Select(refElem => refElem.Value) before iterating the references.

This will give you access to the element itself and should allow for an update of the value for the element. Once you have changed all values you need to, you can write the XMLdoc back to the proj file.

XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
        XDocument projDefinition = XDocument.Load(filePath);
        IEnumerable<string> references = projDefinition
            .Element(msbuild + "Project")
            .Elements(msbuild + "ItemGroup")
            .Elements(msbuild + "Reference")
            .Elements(msbuild + "HintPath");
        foreach(var element in references)
        {
            // do what you need to in order to update the reference
        }
        projDefinition.Save(filePath);
Matt Monty
  • 118
  • 1
  • 7