1

我需要强制使用dll's存储在公共位置的所有项目。我首先重构现有项目以指向dll's我偏好的源文件夹。所以我决定尝试一种编程方式来更改这些引用。

我阅读dll参考文献如下:

 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);

下一步是HintPath根据参考名称更改参考。我计划创建一个参考字典,然后运行它们以更新每个项目文件。

但是,我找不到任何HintPath基于Referenceinside的可靠阅读和更改方式csproj

4

1 回答 1

0

我可能已经很晚了,我还没有测试这个特定的应用程序,但你应该能够.Select(refElem => refElem.Value)在迭代引用之前获取你拥有的代码并删除它。

这将使您可以访问元素本身,并且应该允许更新元素的值。一旦更改了所有需要的值,就可以将 XMLdoc 写回 proj 文件。

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);
于 2017-12-12T11:30:45.393 回答