Creating a New Project

Creating a New Project

To programmatically create a project

  1. Start Visual Studio and create a new Visual Studio add-in project.

  2. Add the code below to the add-in's Connect class.

  3. Run the add-in project and activate it in the Add-In Manager.

    To do this, click Add-In Manager on the Tools menu and then check the box next to the add-in to activate it.

Example

The following example uses GetProjectTemplate and AddFromTemplate to create two new console projects, one Visual Basic and the other Visual C#, in a solution.

 

Visual Basic
 
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/kempb/addins/MyCSProject"
        Dim vbPrjPath As String = _
        "C:/UserFiles/kempb/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
 
C#
 
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//kempb//addins//MyCSProject";
        string vbPrjPath = "C://UserFiles//kempb//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);
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值