简体   繁体   English

以编程方式使用 C# 代码恢复 PackageReferences

[英]Programmatically with C# code restore PackageReferences

I'm trying programmatically with C# base code to restore packageReference for .NET framework & .NET core projects.我正在尝试使用 C# 基本代码编程方式恢复 .NET 框架和 .NET 核心项目的packageReference

I thought about using dotnet.exe / msbuild.exe but I don't know how!我想过使用 dotnet.exe / msbuild.exe 但我不知道如何使用!

I want to simulate what we can do with dotnet CLI :我想模拟我们可以用dotnet CLI做什么:

dotnet restore '.\myproject.csproj' --packages '.\OutputFolder' 

but I want to do it programmatically.但我想以编程方式进行。

Thanks for the answers.感谢您的回答。

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 Directory将 Nuget.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.

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