Udostępnij za pośrednictwem


How to: Create a New Windows Application Using Extensibility

Creating a new project and adding it to the current solution is accomplished through the AddFromTemplate method of the general extensibility DTE.Solution object. This task assumes that you know how to access the Macros integrated development environment (IDE) and create a macro project. For more information, see Automating Repetitive Actions by Using Macros.

The following steps create a Visual Basic project. To create a Visual C# project, use the string "CSharp" in step 4, and to create a Visual J# project, use the string "JSharp".

Note

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, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To add a new project to the solution

  1. Create a new macro module and name it NewProject.

  2. Add a new macro,NewWindowsProject, to the module.

    Sub NewWindowsProject()
       ' Add code here to create new project.
    End Sub
    

    This macro adds a new Visual Basic Windows application.

  3. Select the template you need for a console project. Several different project types may be created, as shown in the table. For a Windows application, use the WindowsApplication.zip template.

    Template Name

    Project Type

    ClassLibrary.zip

    Class library

    ConsoleApplication.zip

    Console application

    EmptyProject.zip

    Empty project

    WebApplication.zip

    Web application

    WebControl.zip

    Web control

    WebService.zip

    Web service

    WindowsApplication.zip

    Windows application

    WindowsControl.zip

    Windows control

    WindowsService.zip

    Windows service

    The syntax for using a template is GetProjectTemplate("WindowsApplication.zip", "VisualBasic"). You can also access templates specific to applications, for example: GetProjectTemplate("PocketPC2003-ClassLibrary.zip", "CSharp") returns the template for a Visual C# class library for a Pocket PC 2003 project. The project templates for all the languages can be found in <drive>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language.

    You can also create your own custom project templates and custom project item templates. To specify the directory in which you will store your templates, click Options on the Tools menu. On the left pane of the Options dialog box, click Projects and Solutions. Type the paths for your templates in the Visual Studio user project templates location and Visual Studio user item templates location boxes. Alternatively, you can accept the default locations.

    Custom templates require unique file names that do not conflict with the file names defined in:

    • <drive>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language

    and

    • <drive>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\Language.

    Ensure that you use long file names (as opposed to 8dot3). For more information, see Creating Project and Item Templates.

  4. Use the GetProjectTemplate method to locate the project template. The template path depends on the language, so to retrieve the Visual Basic template, use the string "Visual Basic", as shown below. For a Visual C# template use the string "CSharp", and for a Visual J# template, use "JSharp".

            Dim vbTemplatePath As String
            Dim vbProjectPath As String
            vbProjectPath = "C:\UserFiles\MyFiles\MyProject"
            vbTemplatePath = soln.GetProjectTemplate("Windows _
     Application", "VisualBasic")
    
  5. Call the AddFromTemplate method.

            ' Create a new solution.
            ' Make sure the filepath below exists
            ' on your computer.
            soln.Create("C:\UserFiles\MyFiles\MyProject", "MySolution")
            ' Create a new VB console project using the template
            ' obtained above.
            soln.AddFromTemplate(vbTemplatePath, vbProjectPath, _
            "VB Console Project", False)
    

    The complete macro appears below:

    Sub NewWindowsProject ()
            'This function creates a solution and adds a Visual Basic Console
            'project to it. 
            Dim soln As Solution2 = CType(DTE.Solution, Solution2)
            'Dim proj As Project
            Dim msg As String
            Dim vbTemplatePath As String
            Dim vbProjectPath As String
            vbProjectPath = "C:\UserFiles\MyFiles\MyProject"
            vbTemplatePath = soln.GetProjectTemplate _
           ("Windows Application", "VisualBasic")
    
            ' Create a new solution.
            ' Make sure the filepath below exists
            ' on your computer.
            soln.Create("C:\UserFiles\MyFiles\MyProject", "MySolution")
            ' Create a new VB console project using the template
            ' obtained above.
            soln.AddFromTemplate(vbTemplatePath, vbProjectPath, _
            "VB Windows Project", False)
            msg = "Created new solution: " & soln.FullName & vbCrLf
            msg = msg & "Created new project: " & soln.Projects.Kind()
            MsgBox(msg)
        End Sub
    
  6. Save the macro, close the Macros IDE, and run the macro from Macro Explorer.

  7. View the new Solution, "MySolution", and the Windows application, "VB Windows Project" in the Solution Explorer.

See Also

Tasks

How to: Migrate Code that Creates Projects by Using Templates

How to: Programmatically Create Projects

How to: Programmatically Create Project Items

Other Resources

Controlling the Solution and Its Projects

Creating Project and Item Templates