简体   繁体   中英

Get nuget package version included in csproj in .Net Core 3.1

In plain words I want to be able to modify.csproj files programmatically.

I am developing an application that replaces a nuget package with its actual project reference. This is application is used for debugging purpose. We have a huge set of packages that we add in our solution file. To do that I read a csproj file and apply a regex to find the specifies nuget package reference and add a project reference instead. This part was working, but its not picking the packages anymore.

The above can be done using the dotnet command but using that I cannot replace the old version back.

Is there any Microsoft nuget package available which can read the package references in csproj files?

Similar question is asked here but Build.Engine is not available in the latest version.

Is there any Microsoft nuget package available which can read the package references in csproj files?

As far as I know , Microsoft.Build.Engine does not support editing New Sdk format projects( Net Core ) and is not compatible with this nuget package. This nuget package is just for old sdk format projects( Net Framework ) and you cannot edit xml elements of Net Core Projects.

Get nuget package version included in csproj in.Net Core 3.1

Since you want to get Nuget package References and their versions, you could try this function programmatically:

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}");
            }


        }

Update 1

Just install Microsoft.Build and Microsoft.Build.Utilities.Core in a net framework project and then type these which can set a property in xxx.csproj file as Build.Engine nuget package did.

Note that it should be written in net framework projects rather than net core or net standard projects(which will cause errors).

using Microsoft.Build.Evaluation;
.......
{
    class Program
    {

        static void Main(string[] args)
        {
            var collection = new ProjectCollection();
            var project = collection.LoadProject(@"xx:\xxx\xxx\xxx.csproj");
            project.SetProperty("Test","true");

            project.Save(@"xx:\xxx\xxx\xxx.csproj");


        }
    }
}

Hope it could help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM