ソリューションファイルへの文字列ファイルのパスを指定します。 C:\ Foo.sln、そのソリューション用のNuGetパッケージをプログラムでどのように復元しますか?私はthis guideに従うことを試みたが、悲惨なことに不完全であることがわかった。C#プログラムでNuGetパッケージを復元する
NuGetパッケージNuGet.VisualStudioを記事の推奨どおりにインストールした後、私はパッケージを復元しようとしています。ここで
は、私がこれまで持っているコードです:// requires reference to System.ComponentModel.Composition
[Import(typeof(IVsPackageRestorer))]
private static IVsPackageRestorer packageInstaller;
し、それを使用しようとしている(EnvDTEリファレンスを必要とするようだ)(とどのように1つの使用この魔法「のgetService」方法ありません - それはから来たんを?):
private static void RestorePackages(string solutionPath)
{
// Using the IComponentModel service
// https://docs.microsoft.com/en-us/nuget/visual-studio-extensibility/nuget-api-in-visual-studio#ivspackagerestorer-interface
// How do I get the project? Can I do this at the solution level or do I have to get all projects under the solution?
EnvDTE.Project project = null;
packageInstaller.RestorePackages(project);
////What is this code??? What is GetService???
////var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
////IVsPackageRestorer packageRestorer = componentModel.GetService<IVsPackageRestorer>();
////packageRestorer.RestorePackages(project);
}
編集/ソリューション:マット・ワードの勧告、への完全なパスに基づいてパッケージを復元するために、私の場合は、以下の方法のようなルックスをnuget.exeする「シェルアウト」するためのコードを使用して溶液。
private static void RestorePackages(string solutionPath)
{
using (Process process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
FileName = "nuget.exe",
Arguments = $"restore {solutionPath}",
UseShellExecute = false,
CreateNoWindow = true
};
process.Start();
process.WaitForExit();
}
}