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

时间:2017-06-01 19:48:40

标签: c# visual-studio

获取所有* .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

1 个答案:

答案 0 :(得分:1)

.configXNamespace 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! } 是xml文件。它们很容易使用linq2xml进行处理。

 using System.Net.Http;