4

For my project, I need to create programmatically a MVC solution and project.

I've already search and I've found a way to create a Console Application using Solution2 and EnvDTE80. This is the code :

string commonName = "ConsoleAppProg";
string csPrjPath = $"C:\\TestCreateProject\\{commonName}";
Type type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
DTE dte = (DTE)Activator.CreateInstance(type, true);
Solution2 sln = (Solution2)dte.Solution;
sln.Create(csPrjPath, commonName);
sln.SaveAs(commonName + ".sln");
string csProjectTemplatePath = sln.GetProjectTemplate("ConsoleApplication.zip", "CSharp");
sln.AddFromTemplate(csProjectTemplatePath, $"{csPrjPath}\\{commonName}", commonName, false);

But I didn't find a way to create a MVC Project. Searching in the Extensions folder of the VS installation folder, I didn't find a "mvc.zip" or something else except ConsoleApplication and ClassLibrary.

I hope someone can help me with this problem!

Thank you in advance !

Axeen
  • 270
  • 1
  • 2
  • 7
  • I see directories under `C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ProjectTemplates`. Did you install any templates? – Dan Wilson Aug 06 '18 at 15:50
  • 1
    It would be so easy if you use ASP MVC app in .NET Core. Just use command `dotnet new mvc -o ProjectName` and that's all. You should consider it :) – Radim Göth Aug 06 '18 at 16:07

1 Answers1

1

Thanks to Dan Wilson in comments, I've searched in this folder (Reminder : C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ProjectTemplates) and I've found a way to perform a MVC project creation

For those who want to develop it, follow this code :

// PATH
string commonName = "ConsoleAppProg";
string csPrjPath = $"C:\\TestCreateProject\\{commonName}";

// SOLUTION INITIALIZATION
Type type = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
DTE dte = (DTE)Activator.CreateInstance(type, true);
Solution2 sln = (Solution2)dte.Solution;

string csProjectTemplatePath = sln.GetProjectTemplate("WebApplication.zip|FrameworkVersion=4.5", "CSharp");

// SOLUTION AND PROJECT CREATION
sln.Create(csPrjPath, commonName);
sln.SaveAs(commonName + ".sln");
sln.AddFromTemplate(csProjectTemplatePath, $"{csPrjPath}\\{commonName}", commonName, false);

Look at the first string sln.GetProjectTemplate(). To create a Web Application based on MVC, you must to specify the Framework Version after the pipe inside the string. In my case : 4.5

Thanks again !

Axeen
  • 270
  • 1
  • 2
  • 7