简体   繁体   English

以编程方式恢复 nuget 包(.net core & .net framework)

[英]Restore nuget packages Programmatically (.net core & .net framework)

I want to restore nuget packages programmatically for .net core and .net Framework based on Packages.config & package references.我想基于Packages.config和包引用以编程方式为 .net core 和 .net Framework 恢复 nuget 包。

Any idea?任何的想法?

Thank you!谢谢!

The nuget.exe tool deals with both cases. nuget.exe 工具处理这两种情况。 So instead of "msbuild /t:Restore" or "dotnet restore", just run "nuget restore".因此,不要运行“msbuild /t:Restore”或“dotnet restore”,只需运行“nuget restore”即可。 If you want to do it programatically, you can use the nuget package Nuget.CommandLine (install via Nuget or Chocolatey, depending on your needs)如果您想以编程方式执行此操作,可以使用 nuget 包 Nuget.CommandLine(通过 Nuget 或 Chocolatey 安装,具体取决于您的需要)

If using shell is ok, You may try following steps to restore nuget packages using c#.如果使用shell没问题,您可以尝试按照以下步骤使用c#恢复nuget包。

  1. Download latest version of Nuget.exe from NuGet Gallery DownloadsNuGet 库下载下载最新版本的 Nuget.exe
  2. Add Nuget.exe to your C# project and mark as " Copy if newer " to Copy to Output DirectoryNuget.exe添加到您的 C# 项目并标记为“如果更新则复制”以复制到输出目录
  3. Run below RestorePackages() method with solution path as parameter在以解决方案路径为参数的RestorePackages()方法下运行

        public static void RestorePackages(string solutionPath)
        {
            var dir = AppDomain.CurrentDomain.BaseDirectory;
            ProcessStartInfo objPI = new ProcessStartInfo($"{dir}\\nuget.exe", $"restore \"{solutionPath}\" -Verbosity quiet");
            objPI.RedirectStandardError = true;
            objPI.RedirectStandardOutput = true;
            objPI.UseShellExecute = false;

            Process objProcess = Process.Start(objPI);
            string error = objProcess.StandardError.ReadToEnd();
            string output = objProcess.StandardOutput.ReadToEnd();

            objProcess.WaitForExit();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NuGet 包在 a.Net Standard 2.0 dll 被.Net Framework 和.Net Core 使用 - NuGet packages in a .Net Standard 2.0 dll used by both .Net Framework and .Net Core .Net Core 2.0在dotnet还原期间警告NU1603,NuGet套件问题 - .Net Core 2.0 Warning NU1603 during dotnet restore, problems with NuGet packages .NET 核心依赖项的 Nuget 包还原路径 - Nuget package restore path for .NET core dependencies 我的 NuGet 包是否与 .NET Core 兼容? - Are my NuGet packages compatible with .NET Core? 搜索.NET Core或标准NuGet包 - Search for .NET Core or Standard NuGet packages .NET Core 如何以编程方式检索具有相同所有者的所有 nuget 包元数据? - .NET Core how to retrieve programmatically all nuget packages metadata with same owner? NuGet恢复后,MSBuild在单元测试项目(.NET Core)上失败 - MSBuild fails on Unit Test Project (.NET Core) after NuGet Restore .net 核心 3.1 Azure devops 管道在 nuget 恢复时失败 - .net core 3.1 Azure devops Pipeline failed on nuget restore 无法在框架 4.7.2 中恢复 nuget 包 - Cant restore nuget packages in framework 4.7.2 针对.net核心和.net框架4.6的Nuget包兼容性 - Nuget package compatibility for .net core and .net framework 4.6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM