How to: Programmatically Create Projects
Visual Studio add-ins are deprecated in Visual Studio 2013. You should upgrade your add-ins to VSPackage extensions. For more information about upgrading, see FAQ: Converting Add-ins to VSPackage Extensions.
To create a project, call GetProjectTemplate, and then pass the returned template paths to AddFromTemplate.
Project templates have a .vstemplate file name extension and are stored in .zip files. To obtain the path of the .vstemplate file (in the .zip file), use GetProjectTemplate, and then pass it to AddFromTemplate to create the project (and also a solution, if one is not already open). You can perform this operation as many times as required, and every project will be added to the currently open solution.
The project templates for all languages can be found in Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\Language\.
You can also create your own custom project templates. To specify the directory in which you will store your templates, click Options on the Tools menu. In the left pane of the Options dialog box, click Projects and Solutions. Type the path of your templates in the Visual Studio user project templates location box.
Custom project templates require unique file names that do not conflict with the file names that are defined in Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\Language\.
Ensure that you use long file names (as opposed to 8dot3). For more information, see Creating Project and Item Templates.
备注
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, click Import and ExportSettings on the Tools menu. For more information, see Customizing Development Settings in Visual Studio.
Start Visual Studio and create a Visual Studio add-in project.
To the add-in Connect class, add the example code shown later in this topic.
Run the add-in project and activate it in Add-In Manager.
To do this, click Add-In Manager on the Tools menu and then select the add-in.
The following example uses GetProjectTemplate and AddFromTemplate to create two console projects, one Visual Basic and the other Visual C#, in a solution.
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
createProjectsFromTemplates(_applicationObject)
End Sub
Sub createProjectsFromTemplates(ByVal dte As DTE2)
Try
' Create a solution with two projects in it, based on project
' templates.
Dim soln As Solution2 = CType(DTE.Solution, _
Solution2)
Dim csTemplatePath As String
Dim vbTemplatePath As String
Dim csPrjPath As String = _
"C:\UserFiles\user1\addins\MyCSProject"
Dim vbPrjPath As String = _
"C:\UserFiles\user1\addins\MyVBProject"
' Get the project template path for a C# console project.
' Console Application is the template name that appears in the
' right pane, "CSharp" is the Language(vstemplate) as seen in
' the registry.
csTemplatePath = soln.GetProjectTemplate _
("ConsoleApplication.zip", "CSharp")
MsgBox("C# template path: " & csTemplatePath)
' Get the project template path for a Visual Basic
' console project.
' "vbproj: is the DefaultProjectExtension as seen in the
' registry.
vbTemplatePath = soln.GetProjectTemplate _
("ConsoleApplication.zip", "vbproj")
MsgBox("Visual Basic template path: " & vbTemplatePath)
' Create a new C# console project using the template obtained
' above.
soln.AddFromTemplate(csTemplatePath, csPrjPath, _
"New CSharp Console Project", False)
' Create a new Visual Basic console project using the template
' obtained above.
soln.AddFromTemplate(vbTemplatePath, vbPrjPath, _
"New Visual Basic Console Project", False)
Catch ex As System.Exception
MsgBox("ERROR: " & ex.ToString)
End Try
End Sub
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
{
// Create a solution with two projects in it, based on project
// templates.
Solution2 soln = (Solution2)dte.Solution;
string csTemplatePath;
string vbTemplatePath;
string csPrjPath = "C:\\UserFiles\\user1\\addins\\MyCSProject";
string vbPrjPath = "C:\\UserFiles\\user1\\addins\\MyVBProject";
// Get the project template path for a C# console project.
// Console Application is the template name that appears in
// the right pane. "CSharp" is the Language(vstemplate) as seen
// in the registry.
csTemplatePath = soln.GetProjectTemplate("ConsoleApplication.zip",
"CSharp");
System.Windows.Forms.MessageBox.Show("C# template path: " +
csTemplatePath);
// Get the project template path for a Visual Basic console
// project.
// "vbproj: is the DefaultProjectExtension as seen in the
// registry.
vbTemplatePath = soln.GetProjectTemplate("ConsoleApplication.zip",
"vbproj");
System.Windows.Forms.MessageBox.Show("Visual Basic template path: " +
vbTemplatePath);
// Create a new C# console project using the template obtained
// above.
soln.AddFromTemplate(csTemplatePath, csPrjPath, "New CSharp
Console Project", false);
// Create a new Visual Basic console project using the template
// obtained above.
soln.AddFromTemplate(vbTemplatePath, vbPrjPath, "New VB Console
Project", false);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message);
}
}
How to: Compile and Run the Automation Object Model Code Examples
How to: Programmatically Create Project Items
Manipulating Visual Basic and Visual C# Projects