[英]How to install Nuget Packages using ENVDTE.DTE
I created one console application to create visual studio project pragmatically, here i am not able install Nuget packages, always var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); 我创建了一个控制台应用程序来实用地创建Visual Studio项目,在这里我无法安装Nuget包,总是var componentModel =(IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); statement returns null values. 语句返回空值。 for your reference i added my code below. 供您参考,我在下面添加了我的代码。 Help me to resolve this issue. 帮助我解决此问题。
static void Main(string[] args)
{
//InstallNuGetPackages.InstallNuGet("");
string ProjectName = "WebAPIProj";
string SolutionName = "EmptyTemplate";
System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
Object obj = System.Activator.CreateInstance(type, true);
EnvDTE.DTE dte = (EnvDTE.DTE)obj;
dte.MainWindow.Visible = true; // optional if you want to See VS doing its thing
// create a new solution
dte.Solution.Create("C:\\"+ SolutionName + "\\", SolutionName);
var solution = dte.Solution;
// create a C# WinForms app
solution.AddFromTemplate(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ProjectTemplatesCache\CSharp\Web\1033\EmptyWebApplicationProject40\EmptyWebApplicationProject40.vstemplate",
@"C:\NewSolution\"+ ProjectName, ProjectName);
InstallNuGetPackages.InstallNuGet(dte);
foreach (var p in dte.Solution.Projects)
{
InstallNuGetPackages.InstallNuGet((Project)p, "Microsoft.AspNet.WebApi version1.1");
}
// save and quit
dte.ExecuteCommand("File.SaveAll");
dte.Quit();
}
Code to install Nuget Packages 安装Nuget软件包的代码
public bool InstallNuGetPackage(Project project, string package)
{
bool installedPkg = true;
try
{
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); //Always this statement returns null
IVsPackageInstallerServices installerServices = componentModel.GetService();
if (!installerServices.IsPackageInstalled(project, package))
{
var installer = componentModel.GetService();
installer.InstallPackage(null, project, package, (System.Version)null, false);
}
}
catch (Exception ex)
{
installedPkg = false;
}
return installedPkg;
}
(Turned this into an answer for better readability and more room) (将其转换为更好的可读性和更多空间的答案)
created one console application
- you only have access to the ServiceProvider
from Visual Studio if you run your code inside of it, ie from an extension and/or package . created one console application
-你只需要访问ServiceProvider
从Visual Studio,如果你在它的内部从运行您的代码,即扩展和/或包装 。
Running this from a console application cannot work. 从控制台应用程序运行此命令将无法进行。 Visual Studio internally does a lot more setup for all the services and general environment than creating an instance of DTE
. Visual Studio的内部确实为所有的服务和比创建的实例一般环境有更多的设置DTE
。
To persue your route, although I'm not sure how feasible that is, invoke nuget.exe
or NuGet.Core
code to achieve similar. 要说服您的路线,尽管我不确定那是否可行,请调用nuget.exe
或NuGet.Core
代码来实现类似的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.