shepherd324
shepherd324

Reputation: 27

Automate Creating Visual Studio Project

I would like to automate the process of creating a Visual Studio 2013 project (specifically, an MVC 4 web application) via a powershell script and I am not finding any useful information on how to do this.

I understand I could take a the project template xml/scaffolding files and copy them and replace the namespace/project tokens etc, however VS does all of this perfectly already so it seems there should be an API that could be used to do so.

Sometimes I see references to an old tool called projectgen.exe that seems to be a VS 2008 artifact - I cannot find it for VS 2013, and I'm not even sure if it actually does what I'm after.

Upvotes: 1

Views: 593

Answers (1)

MvdD
MvdD

Reputation: 23486

One way to do it is to have Visual Studio do it for you, using automation.

The code should look something like:

{
    Type type = Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
    var visualStudio = Activator.CreateInstance(type, true);

    var dte = visualStudio.Instance;
    var solution = (Solution2)dte.Solution;
    solution.Create(destinationFolder, solutionName);
    string templateFile = solution.GetProjectTemplate(applicationProjectTemplateName);
    var project = solution.AddProjectFromTemplate(projectName, DestinationFolder, templateFile);
    visualStudio.SaveAll();
}

Upvotes: 1

Related Questions