简体   繁体   中英

Is there any way to check if a NuGet package support .Net standard in command line?

I have a list of more then 100 NuGet packages my project depends on. Is there any way to check which of these packages support .Net standard and which do not (without going through them one by one and check)?

You can use NuGet package libraries to read the frameworks from any package if you have it on your disk. Consider the following code as a sample -

class Program
{
    static void Main(string[] args)
    {
        var directory = @"F:\validation\test-local-source";
        foreach (var file in Directory.GetFiles(directory, "*.nupkg", SearchOption.AllDirectories))
        {
            CheckPackage(file);
        }
    }

    private static void CheckPackage(string path)
    {
        using (var package = new PackageArchiveReader(path))
        {
            var frameworks = package
                .GetSupportedFrameworks()
                .Where(f => f.DotNetFrameworkName.IndexOf(".NETStandard", StringComparison.OrdinalIgnoreCase) >= 0);

            if (frameworks.Any())
            {
                Console.WriteLine($"{Path.GetFileNameWithoutExtension(path)}: {string.Join(";", frameworks)}");
            }
            else
            {
                Console.WriteLine($"{Path.GetFileNameWithoutExtension(path)}: ERROR");
            }
        }
    }
}

Here we loop through each package in a directory in the main method and check each package. For each package we parse it into a PackageArchiveReader that reads a package file and extracts all the metadata in the package. Then you can query for the frameworks supported by the package.

You will need the following references to run this code -

using NuGet.Packaging;
using System;
using System.IO;
using System.Linq;

The only thing remaining is that you have all the packages in a common directory. If you are using packages.config type of project then you can open the solution directory and you should have a packages directory. Pass that as the root directory.

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