简体   繁体   English

动态创建解决方案文件(.sln)和项目文件(.csproj)

[英]Create solution file(.sln) and project files(.csproj) dynamically

I am creating code generation tool (auto code generation based on table structure) as a Windows forms application in Visual Studio 2012 using .NET Framework 4.0. 我正在使用.NET Framework 4.0在Visual Studio 2012中创建代码生成工具(基于表结构的自动代码生成)作为Windows窗体应用程序。 It's generating the portable object, controller, WCF services and business logic code files. 它正在生成可移植对象,控制器,WCF服务和业务逻辑代码文件。

All code files bundle in the appropriate project and all project bundle in one solution. 所有代码文件都捆绑在适当的项目中,而所有项目捆绑在一个解决方案中。 The solution and projects need to create dynamically through program. 解决方案和项目需要通过程序动态创建。

I have tried to create the solution and project using Visual Studio Add-in project. 我试图使用Visual Studio加载项项目创建解决方案和项目。 It is working fine in Add-In project (separate solution). 在外接程序项目(单独的解决方案)中,它运行良好。 The OnConnection method call automatically in Add-in project. Add-in项目中自动调用OnConnection方法。 Now I want to implements this in my code generation tool. 现在,我想在我的代码生成工具中实现这一点。 While debugging in Add-In project the application variable shown like COM object . Add-In application项目中调试时, application变量显示为COM object

I am tried to pass the value for OnConnection method from code generation tool, it throws an error (I passed this object for application variable). 我试图从代码生成工具传递OnConnection方法的值,它引发错误(我this对象传递给application变量)。 I really don't know how to call this method from my code generation tool. 我真的不知道如何从我的代码生成工具中调用此方法。 Anyone help this? 有人帮忙吗?

Code

 private DTE2 _applicationObject;
 private AddIn _addInInstance;

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    createProjectsFromTemplates(_applicationObject);
}


public void createProjectsFromTemplates(DTE2 dte)
{
    try
    {
        Solution2 soln = (Solution2)dte.Solution;
        string csTemplatePath;
        string csPrjPath = "SamplePath\\TestCreateProject";
        csTemplatePath = soln.GetProjectTemplate("WpfApplication.zip", "CSharp");
        System.Windows.Forms.MessageBox.Show("C# template path: " + csTemplatePath);

        soln.AddFromTemplate(csTemplatePath, csPrjPath, "NewWCFCSharpAutoGeneratorProject", false);

        Project prj;
        ProjectItem prjItem;

        String itemPath;
        // Point to the first project (the Visual Basic project).
        prj = soln.Projects.Item(1);

        prjItem = prj.ProjectItems.AddFromFileCopy("SampelCSharp.cs");
    }
    catch (System.Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message);
    }
}

You can instantiate a VS from the host application and generate the files. 您可以从主机应用程序实例化VS并生成文件。 Hope that will work. 希望能奏效。 The below code works well for me. 以下代码对我来说效果很好。

Use the following namespaces to get work the below given code. 使用以下名称空间可以使用以下给定的代码进行工作。

Namespaces: 命名空间:

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;

Code: 码:

EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.11.0");

Connect objConnect = new Connect();
Array objArray = null;
objConnect.OnConnection(dte2, ext_ConnectMode.ext_cm_UISetup, null, ref objArray);

You can use this. 您可以使用它。 This is for .cs project files and framewwork above .NET 2.0 versions. 适用于.NET 2.0以上版本的.cs项目文件和框架。 VB project sources are not compatible. VB项目源不兼容。

protected void Build(string project)
    {


        Engine engine = new Engine();

        BuildPropertyGroup properties = new BuildPropertyGroup();

        properties.SetProperty(@"Configuration", @"Debug");


        // Point to the path that contains the .NET Framework 2.0 CLR and tools
        engine.BinPath = @"c:\windows\microsoft.net\framework\v3.5";

        // Instantiate a new FileLogger to generate build log
        FileLogger logger = new FileLogger();

        // Set the logfile parameter to indicate the log destination
        string str   = @"logfile=D:\temp";
               str  += project.Substring(project.LastIndexOf("\\"), project.LastIndexOf(".") - project.LastIndexOf("\\")) + ".log";
        logger.Parameters = str;

        // Register the logger with the engine
        engine.RegisterLogger(logger);


        // Build a project file
        bool success = engine.BuildProjectFile(project, new string[] { "build" }, properties);

        //Unregister all loggers to close the log file
        engine.UnregisterAllLoggers();

        using (BinaryWriter writer = new BinaryWriter(File.Open(@"D:\temp\Prj.log", FileMode.Append)))
        {
            if (success)
            {
                writer.Write("\nBuild Success :" + project.Substring(project.LastIndexOf("\\")));
            }
            else
            {
                writer.Write("\nBuild Fail :" + project.Substring(project.LastIndexOf("\\")));
            }
        }
    }

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

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