简体   繁体   English

项目模板DTE Visual Studio 2017 nuget

[英]Project Template DTE Visual Studio 2017 nuget

I try to create a project automated with DTE this work perfect but i cannot add a nuget package... 我尝试创建一个使用DTE自动化的项目,但效果很好,但是我无法添加nuget包...

Option1 (InstallNuGetPackage code below) Option1(下面的InstallNuGetPackage代码)

var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
//componentModel is always null

I have installed this nuget package 我已经安装了这个nuget包

  • NuGet.VisualStudio 4.0.0 NuGet.VisualStudio 4.0.0

And add following framework references 并添加以下框架参考

  • Microsoft.VisualStudio.ComponentModelHost 15.0.0.0 Microsoft.VisualStudio.ComponentModelHost 15.0.0.0
  • Microsoft.VisualStudio.Shell.15.0 15.0.0.0 Microsoft.VisualStudio.Shell.15.0 15.0.0.0

I have found this example but is not work http://tylerhughes.info/archive/2015/05/06/installing-a-nuget-package-programmatically/ 我找到了这个示例,但无法正常使用http://tylerhughes.info/archive/2015/05/06/installing-a-nuget-package-programmatically/

Option2 (Add a own package.config) Option2(添加自己的package.config)

I have also try with creating the packages.config xml but then i have no references to this package and must edit the csproj... 我也尝试创建packages.config xml,但是我没有对此包的引用,必须编辑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
}

Visual Studio control Visual Studio控件

var type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
var obj = Activator.CreateInstance(type, true);
this._applicationObject = (DTE2)obj;

InstallNuGetPackage 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;
}

Create Project 建立专案

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);
    }
}

With this example you can open the package manager console window and send a install-package command. 在此示例中,您可以打开软件包管理器控制台窗口并发送install-package命令。

var packageManagerConsoleGuid = "{0AD07096-BBA9-4900-A651-0598D26F6D24}";
var window = this._visualStudioInstance.Windows.Item(packageManagerConsoleGuid);
window.Activate();

var commandName = "View.PackageManagerConsole";
var nugetCommand = "install-package log4net -ProjectName DemoProject";

this._visualStudioInstance.ExecuteCommand(commandName, nugetCommand);

I develop a project to automate create solution with projects you can found it here Nager.TemplateBuilder 我开发了一个项目,用于使用项目自动创建解决方案,您可以在这里找到它Nager.TemplateBuilder

This example create a Windows Desktop Application with two nuget packages 本示例使用两个nuget包创建Windows桌面应用程序

//Configure Project
var demoProject = new ProjectInfo($"DemoProject", ProjectTemplate.WindowsClassicDesktopWindowsFormsApp);
demoProject.NugetPackages = new List<string> { "System.ServiceModel.NetTcp", "System.Runtime.Serialization.Xml" };

//Configure Solution
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var solutionInfo = new SolutionInfo("Test", folder);
solutionInfo.ProjectInfos.Add(demoProject);

//Start building machine
var buildingMachine = new SolutionBuildingMachine();
buildingMachine.Build(solutionInfo);
buildingMachine.Dispose();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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