【问题标题】:Restore nuget packages Programmatically (.net core & .net framework)以编程方式恢复 nuget 包(.net 核心和 .net 框架)
【发布时间】:2019-08-09 11:21:08
【问题描述】:

我想基于 Packages.config 和包引用以编程方式为 .net 核心和 .net Framework 恢复 nuget 包。

有什么想法吗?

谢谢!

【问题讨论】:

标签: c# msbuild nuget nuget-package-restore dotnet-restore


【解决方案1】:

nuget.exe 工具处理这两种情况。因此,不要使用“msbuild /t:Restore”或“dotnet restore”,只需运行“nuget restore”即可。如果您想以编程方式执行此操作,可以使用 nuget 包 Nuget.CommandLine(通过 Nuget 或 Chocolatey 安装,具体取决于您的需要)

【讨论】:

    【解决方案2】:

    如果使用shell没问题,您可以尝试以下步骤使用c#恢复nuget包。

    1. NuGet Gallery Downloads下载最新版本的Nuget.exe
    2. Nuget.exe 添加到您的 C# 项目并标记为“Copy if newer”以复制到输出目录
    3. 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();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2019-09-01
      • 2018-04-16
      相关资源
      最近更新 更多