【问题标题】:Programatically modifying DLL references inside project file以编程方式修改项目文件中的 DLL 引用
【发布时间】:2016-04-12 13:38:34
【问题描述】:

我需要强制我的所有项目使用存储在公共位置的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。我计划创建一个引用字典,然后遍历它们以更新每个项目文件。

但是,我找不到任何基于Reference 内部csproj 的可靠方法来阅读和更改HintPath

【问题讨论】:

  • 查看类似问题的答案:stackoverflow.com/a/35233082/33082
  • 答案准确地显示了我所做的,加上它将读取到的引用转储到 txt。不是很有用。我需要的是能够根据 Reference 修改 HintPath。

标签: c# reference msbuild csproj projects-and-solutions


【解决方案1】:

我可能已经很晚了,我还没有测试这个特定的应用程序,但你应该能够在迭代引用之前获取你拥有的代码并删除.Select(refElem =&gt; 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    相关资源
    最近更新 更多