以编程方式修改项目文件中的DLL引用

时间:2016-04-12 13:38:35

标签: c# reference msbuild csproj project-files

我需要强制我的所有项目都使用存储在公共位置的Interface Builder。我首先重构现有项目以指向我偏好的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,我找不到任何可靠的阅读和更改Reference的方式。

1 个答案:

答案 0 :(得分: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);