Creating new VS2015 solutions with 2 projects programmatically using C#

1

I am using the following code to try to programmatically create Visual Studio 2015 projects on the fly from within an existing Console Application. Why you ask? We are putting together a library of all our code snippets and we want a test/proof test solution for each one, and since we have hundreds of these, we aren't going to do it manually.

Ultimately this will be in an MVC5 app as its own project (class library?) but for now we are just trying to get it functional within this console application.

I am trying to create a new solution with 2 projects (a console application and a unit test project).I am also not even sure I need the unit test project, or whether one even works with a console app since there is no option to add a unit test to a console app in VS2015 solution generation.

Here is the code;

public class CreateConsoleProjectsProgrammatically
{
    private const string Vs2015Type = "VisualStudio.DTE.14.0";
    private const string Vs2015ConsoleProject = @"X:\Code Library\Helpers\ConsoleApplication\csConsoleApplication.vstemplate";
    private const string Vs2015UnitTestProject = @"X:\Code Library\Helpers\UnitTestProject\UnitTestProject.vstemplate";
    private const string Vs2015CodeLibraryBasepath = @"X:\Code Library";

    public static void CreateVsConsoleProjectProgrammatically(string filename)
    {
        // Create a solution with two projects in it, based on project
        // templates, a console project and a unit test project.
        var vsType = Type.GetTypeFromProgID(Vs2015Type);

    //error line is below
        var vsInstance= Activator.CreateInstance(vsType, true);

        EnvDTE.DTE dte = (EnvDTE.DTE)vsInstance;
        dte.MainWindow.Visible = false; // optional: set to true if you want to see VS doing its thing

        // create a new solution
        dte.Solution.Create(@"X:\Code Library\", filename);
        var solution = dte.Solution;

        // create a C# console application
        solution.AddFromTemplate(Vs2015ConsoleProject,Path.Combine(Vs2015CodeLibraryBasepath,filename), filename);

        // create a unit test project
        solution.AddFromTemplate(Vs2015UnitTestProject, Path.Combine(Vs2015CodeLibraryBasepath, filename + "_Test"), filename + "_Test");

        // save and quit
        dte.ExecuteCommand("File.SaveAll");
        dte.Quit();
    }
}

Here is the error which is from the Activator.CreateInstance

Creating an instance of the COM component with CLSID {A2FA2136-EB44-4D10-A1D3-6FE1D63A7C05} from the IClassFactory failed due to the following error: 8001010a The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)).

Not sure why I am getting a server busy error. It almost seems like its because VS is already open? But how else would the new solution be generated? I tried closing the solution and reopening it but that did nothing to solve the issue.

I also thought it may have been a connectivity issue, so I moved all the files and directories to C:\ but it produced the same error, so it wasn't an issue of using a networked drive location.

I also tried using

EnvDTE80.DTE2 dte2 = (EnvDTE100.DTE2)vsInstance;

but DTE2 is not an available property/method on EnvDTE100 according to the editor, even though I found some examples using it on the net.

c#
visual-studio-2015
project
asked on Stack Overflow Sep 4, 2016 by dinotom • edited Sep 5, 2016 by dinotom

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0