Search code examples
c#winformsvisual-studio-2010visual-studio-2013visual-studio-addins

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. It's generating the portable object, controller, WCF services and business logic code files.

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. It is working fine in Add-In project (separate solution). The OnConnection method call automatically in Add-in project. Now I want to implements this in my code generation tool. While debugging in Add-In project the application variable shown like 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). 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);
    }
}

Solution

  • I got this reference it is really useful.

    http://rcos.rpi.edu/projects/unmake/commit/programmatically-launch-devenv-generate-a-solution-and-save-it/