2017-11-30 33 views
1

ソリューションファイルへの文字列ファイルのパスを指定します。 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(); 
    } 
} 

答えて

1

コードの実行元が不明です。 Visual Studio APIを使用する代わりに、単にNuGet.exeにシェルしてソリューションを復元する方が簡単かもしれません。リンク先のAPIは、Visual Studio内で拡張機能として実行している場合や、Package Manager Consoleウィンドウなどで実行している場合にのみ使用できます。

あなたが書いているコードがVisual Studio内で実行されていない場合、それがコンソールアプリケーションであるとすれば、nuget.exeを実行する方が簡単になります。それ以外の場合は、使用可能なさまざまなNuGetパッケージを見て、さまざまなNuGet操作を実行するためのAPIを提供することができます。しかし、nuget.exeの復元を実行する方がはるかに簡単です。

関連する問題