Share via


Controlling the Solution and Its Projects

One solution can be open in Visual Studio .NET at any given time, and the solution must contain one or more projects. The projects can be of various types in various languages. For example, a solution might contain a Visual Basic .NET Windows Application project and a Visual C++ .NET ATL Project. Each project contains one or more project items, such as classes, modules, forms, and so forth.

The Visual Studio .NET automation model offers objects to control all of these elements. The key object for controlling a solution is the Solution object. Using it, you can:

  • Create new solutions.
  • Add new projects to the solution, based on the Visual Studio .NET templates.
  • Add existing projects to the solution from files.
  • Remove projects from the solution.
  • Open, save, and close the solution.
  • Obtain information about the solution and the projects and project items it contains.

Controlling Projects and Project Items

Visual Studio .NET provides a generic project model, represented by the following:

Object Name Description
Projects collection Represents all projects in the solution.
Project object Represents a project in the solution.
ProjectItems collection Represents all of the items in a specified project.
ProjectItem object Represents an item in a specified project.

These objects allow you to construct an Add-in that works with any language in Visual Studio .NET. Using them, you can:

  • Save or delete a project.
  • Create a new project item for a project, based on the Visual Studio .NET templates.
  • Add project items to a project from existing files.
  • Remove project items from a project.
  • Open, save, and delete project items from a project.

Note   In newer versions of Visual Studio .NET, special handling for the Project.ProjectItems collection for Visual C++ ® .NET is no longer required. That is, while the Visual C++ ® .NET ProjectItems collection previously stored all Visual C++ ® .NET project files in a flat list, now the files are stored hierarchically as they are in the other programming languages.

Since this change can affect your existing code, there is a way to emulate the old behavior in the new project-specific object model when trying to index the Project.ProjectItems collection to determine whether or not a file is in the project. The primary difference is that you can now return to the DTE object model by calling .Object on a Visual C++ ® .NET object.

Dim proj as VCProject = DTE.ActiveSolutionProjects(0).Object
Dim fileColl as IVCCollection = proj.Files
Dim file as VCFile = fileColl.Item("MyFile.cpp")
Dim projItem as ProjectItem = file.Object

Language-Specific Project Objects

In addition to the generic project-related objects, each language service provides its own custom project-related objects, namely:

Object Name Description
VBProject object Represents Visual Basic .NET projects.
VCProject object Represents Visual C++ .NET projects.
CSharpProject object Represents Visual C# .NET projects.

For example, DTE.VBProjects or DTE.GetObject("VBProjects") returns an object that implements the generic Projects object.

Each language-specific project type offers functionality and features specific to that language, and as such, are good alternatives to the generic project objects if you plan to create an Add-in that is used only with a specific language.

Programatically Creating Projects and Adding Items

Nearly all of the examples in this topic demonstrate how to create new projects using hard-coded paths to the appropriate templates. This is not always desirable, however, because parts of the path might change after the code is written. An alternative means of adding projects, project items, and so forth is to use the project location and template paths stored in Visual Studio .NET. Using these paths with the AddFromTemplate method, you can programmatically create projects.

For example, rather than adding a Visual Basic .NET console application project using a hard-coded path, as in:

proj = soln.AddFromTemplate("C:\Program Files\Microsoft Visual Studio .NET\Vb7\VBWizards\ConsoleApplication\Templates\1033\ConsoleApplication.vbproj", "c:\temp", "My New Project", True)

you could instead use:

Dim strTemplatePath As String = DTE.Solution.TemplatePath(VSLangProj. _
  PrjKind.prjKindVBProject) & "ConsoleApplication.vsz"
Dim strProjectName As String = "MyProject"
Dim strProjectLocation As String = DTE.Properties("Environment", _
  "ProjectsAndSolution").Item("ProjectsLocation").Value
strProjectLocation = strProjectLocation & "\test\" & strProjectName
DTE.Solution.AddFromTemplate(strTemplatePath, strProjectLocation, _
  strProjectName, True)

In this example, the project template path is constructed by specifying the type constant of project and the wizard (.vsz) file to use, and a project location is created using the current project location set up for Visual Studio .NET. After creating a unique project name using the current date and time, the project is created by calling AddFromTemplate and passing in the project and template names and locations.

Another method of programmatically creating a project without using hard-coded paths is to use the LaunchWizard method. LaunchWizard has two parameters: a path to the .vsz file, and context parameters to pass to the wizard. You can set a reference to the .vsz file using the technique outlined previously in strTemplatePath. For details about how to use LaunchWizard, see LaunchWizard Method (DTE Object).

Using these methods to avoid using hard-coded paths can make your code more portable.

Project Unique Name

When using Project object members to refer to projects, such as Solution.Projects.Item, you cannot refer to the project only by the name as it appears in Solution Explorer; you must use the project's unique name. The unique name is a relative pathname from the directory containing the solution (.sln) file to the project file.

For example, if you had the following solution/project structure:

SomeSolution.sln

     WinApp1

          WinApp1.VBProj

Then the unique name for the project would be "WinApp1/WinApp1.VBProj."

Manipulating Solution and Projects Example

The following VSA example demonstrates how to manipulate solutions and projects in Solution Explorer. First, run Soln_Create() to create a solution and a project. Next, close the solution and run Soln_Kill() to remove the project and close the solution.

Sub Soln_Create()
   'This function creates a solution and adds a Visual Basic .NET Console
   'project to it. 
   Dim soln As Solution
   Dim proj As Project
   Dim msg As String

   'Create a reference to the solution.
   soln = DTE.Solution

   ' Create a new solution.
   soln.Create("c:\temp2", "MyNewSolution")

   ' Create a new VB Console application project. Adjust the save path as 
   'needed.
   proj = soln.AddFromTemplate("C:\Program Files\Microsoft Visual Studio .NET\Vb7\VBWizards\ConsoleApplication\Templates\1033\ConsoleApplication.vbproj", "c:\temp", "My New Project", True)
   ' Save the new solution and project.
   soln.SaveAs("c:\temp\newsolution.sln")
   msg = "Created new solution: " & soln.FullName & vbCrLf
   msg = msg & "Created new project: " & proj.Name
   MsgBox(msg)
End Sub

Sub Soln_Kill()
   'This function loads a solution, deletes the first project,
   'and then closes the solution.
   Dim soln As Solution
   Dim proj As Project
   Dim msg As String

   'Create a reference to the solution.
   soln = DTE.Solution

   ' Open the solution just created.
   soln.Open("c:\temp\newsolution.sln")

   ' Delete the newly-created VB Console application project in 
   'this solution.
   MsgBox("Ready to delete the project.")
   proj = soln.Projects.Item(1)
   soln.Remove(proj)
   MsgBox("Project was deleted from the solution.")

   ' Close the solution from the IDE.
   soln.Close()
End Sub

See Also

Introduction to Solutions, Projects, and Items | Adding and Handling Commands | Creating and Controlling Environment Windows | Creating Add-Ins and Wizards | Creating an Add-In | Creating a Wizard | Automation and Extensibility Reference | Automation Object Model Chart