je tente de créer un projet automatisé avec DTE ce travail parfait, mais je ne peux pas ajouter un paquet NuGet ...Modèle de projet DTE Visual Studio 2017 NuGet
Option1 (code InstallNuGetPackage ci-dessous)
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
//componentModel is always null
J'ai installé ce paquet NuGet
- NuGet.VisualStudio 4.0.0
Et ajouter des références suivantes cadre
- Microsoft.VisualStudio.ComponentModelHost 15.0.0.0
- Microsoft.VisualStudio.Shell.15.0 15.0.0.0
J'ai trouvé cet exemple, mais est pas un travail http://tylerhughes.info/archive/2015/05/06/installing-a-nuget-package-programmatically/
Option2 (Ajouter un propre package.config)
je aussi essayer de créer le xml packages.config mais je n'ai aucune référence à ce paquet et doit modifier le csproj ...
public string GetPackagesConfig()
{
var sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.AppendLine("<packages>");
sb.AppendLine("<package id=\"log4net\" version=\"2.0.8\" targetFramework=\"net461\" />");
sb.AppendLine("</packages>");
return sb.ToString();
//Add file to project
}
contrôle Visual Studio
var type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
var obj = Activator.CreateInstance(type, true);
this._applicationObject = (DTE2)obj;
InstallNuGetPackage
public bool InstallNuGetPackage(EnvDTE.Project project, string package)
{
bool installedPkg = true;
try
{
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
IVsPackageInstallerServices installerServices = componentModel.GetService<IVsPackageInstallerServices>();
if (!installerServices.IsPackageInstalled(project, package))
{
var installer = componentModel.GetService<IVsPackageInstaller>();
installer.InstallPackage(null, project, package, (System.Version)null, false);
}
}
catch (Exception ex)
{
installedPkg = false;
}
return installedPkg;
}
Créer un projet
private void CreateProject(string projectSubFolder, string projectName)
{
Solution2 solution2;
string solutionFileFullName;
string solutionFolderFullName;
string projectFolderFullName;
try
{
solution2 = (Solution2)_applicationObject.Solution;
// Get the full name of the solution file
solutionFileFullName = solution2.FileName;
// Get the full name of the solution folder
solutionFolderFullName = Path.GetDirectoryName(solutionFileFullName);
// Compose the full name of the project folder
projectFolderFullName = Path.Combine(solutionFolderFullName, projectSubFolder);
if (!(projectFolderFullName.EndsWith("\\")))
{
projectFolderFullName += "\\";
}
var programfiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
var template = @"Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates\CSharp\Windows\1033\ClassLibrary\csClassLibrary.vstemplate";
var projectTemplateFileName = Path.Combine(programfiles, template);
// Add the project
solution2.AddFromTemplate(projectTemplateFileName, projectFolderFullName, projectName, false);
//Save
_applicationObject.Solution.SaveAs(_solutionFullFileName);
}
catch (Exception exception)
{
Log.Error(nameof(CreateProject), exception);
}
}