簡體   English   中英

以編程方式更改* .csproj和packages.config中的引用

[英]programmatically changing references in *.csproj and packages.config

提取了所有* .csproj和packages.config文件,並想知道什么是更新引用的最佳方法

 var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
                    .Where(s => s.EndsWith(".csproj") || s.EndsWith("packages.config")).ToList();


<Reference Include="MyDev.Something">
  <HintPath>..\..\..\..\Packages\MyDev.Something.1.0\lib\net45\MyDev.Something.dll</HintPath>
  <Private>True</Private>
</Reference>

我想將1.0更改為2.0

.csproj.config是xml文件。 使用linq2xml可以輕松處理它們。

XNamespace ns = @"http://schemas.microsoft.com/developer/msbuild/2003";

foreach (var file in files)
{
    var xml = XElement.Load(file);

    var nodes = xml.Descendants(ns + "Reference")
        .Where(r => r.Attribute("Include").Value.Contains("MyDev.Something"));

    foreach (var node in nodes)
    {
        var hintPath = node.Element(ns + "HintPath");

        if (hintPath.Value.Contains("MyDev.Something.1.0"))                
            hintPath.Value = hintPath.Value.Replace(
                "MyDev.Something.1.0", "MyDev.Something.2.0");
    }

    xml.Save(file); // First make a backup!
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM