。net Core 3.1中的csproj中包含获取nuget软件包的版本

问题描述 投票:0回答:1

简单地说,我希望能够以编程方式修改.csproj文件。

我正在开发一个用实际项目参考替换nuget包的应用程序。这是用于调试目的的应用程序。我们在解决方案文件中添加了很多软件包。为此,我读取了一个csproj文件并应用一个正则表达式来查找指定的nuget包引用,并添加一个项目引用。此部分正在工作,但不再选择软件包。

以上操作可以使用dotnet命令完成,但使用该命令无法将旧版本替换回原先的版本。

有没有可用的Microsoft nuget软件包可以读取csproj文件中的软件包引用?

提出类似问题here,但是Build.Engine在最新版本中不可用。

c# .net-core nuget visual-studio-2019 projects-and-solutions
1个回答
0
投票

是否有可用的Microsoft nuget软件包可以读取csproj文件中的包引用?

据我所知Microsoft.Build.Engine不支持编辑新的Sdk格式项目(Net Core),并且与该nuget包不兼容。此nuget软件包仅适用于旧的sdk格式的项目(Net Framework),您不能编辑Net Core项​​目的xml元素。

到目前为止,没有用于Net Core的这种类型的nuget程序包。

获取.Net Core 3.1中的csproj中包含的nuget软件包版本

[Since您想获取Nuget包引用及其版本,您可以通过编程方式尝试使用此功能:

public class PackageReference
        {
            public string Include { get; set; } //get the nuget reference name
            public Version Version { get; set; } // get thee nuget package version
        }


        static void Main(string[] args)
        {

    //load the xxx.csproj file
            var doc = XDocument.Load("C:\\xxxx\\xxxx\\xxx\\xxx\\xxx.csproj");
            var packageReferences = doc.XPathSelectElements("//PackageReference")
                .Select(pr => new PackageReference
                {
                    Include = pr.Attribute("Include").Value,
                    Version = new Version(pr.Attribute("Version").Value)
                });

            Console.WriteLine($"Project file contains {packageReferences.Count()} package references:");
            foreach (var packageReference in packageReferences)
            {
                Console.WriteLine($"{packageReference.Include}, version {packageReference.Version}");
            }


        }

希望它可以帮助您。

© www.soinside.com 2019 - 2024. All rights reserved.