WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Developing Visual Studio Project Wizards
Pages: 1, 2, 3, 4, 5, 6

The Project Interface Object

The EnvDTE.Project interface represents the Visual Studio project. Visual Studio passes a reference to the Project created by a template to the ProjectFinishedGenerating method. In addition, if your wizard needs to work with the Project object before it is passed a reference to it, it can retrieve the Project object by name from the Projects collection of the Solution object returned by the DTE2.Solution property. The following members of the Project object are especially important in wizard development:

  • CodeModel property: Returns an EnvDTE.CodeModel object representing the project's source code. You can access, modify, or add to the project's source code by navigating this portion of the object model.
  • FullName Property: The path and name of the project in the filesystem. If the project has not yet been saved, its path is a subdirectory of the Visual Studio temporary projects directory.
  • Kind Property: A GUID indicating the project type. The GUID values are stored as subkeys of the HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\8.0\Policy\SupportedProjects key in the registry. The default value of the subkey indicates the project type (such as "VB Project," "CSharp Project," "VC Project," and "Solution Folder Project").
  • Name property: The name of the project.
  • Object property: Returns an Object that can be accessed using the COM QueryInterface method. The object returned by this property can be cast to a VSProject interface object. See "The VSProject Interface Object" later in this article for a discussion of its members.
  • ProjectItems property: Returns a ProjectItems collection object that allows the wizard to access each item in the project. The ProjectItem interface object is described briefly in the next section.
  • Properties property: Returns a Properties collection object that provides access to project-level properties. The Properties collection makes most of the items found in the project's Properties dialog programmatically accessible to your code. Each member of the collection is represented by an EnvDTE.Property object. Some of the useful properties found in this collection are listed in Table 1.
  • Table 1. Selected Property objects in the Project object's Properties collection.

    Name Type Description
    ComVisible Boolean Indicates whether the project's assembly is made accessible through COM interop.
    FullPath String The fully qualified path to the project directory.
    OutputFilename String The name of the assembly that Visual Studio generates for the project.
    RootNamespace String The root namespace for all types defined in the project.
    SignAssembly Boolean Indicates whether the assembly that Visual Studio generates for the project is to be signed.
    StartupObject String The name of the project's startup object or entry point.

  • Saved property: Returns a Boolean that indicates whether the project has not been modified since it was last saved or opened.
  • Delete method: Deletes the current project.
  • Save method: Saves the current project or a designated item in the project.
  • SaveAs method: Saves the current project or a designated item in the project using a specified filename.

The ProjectItem Interface Object

The EnvDTE.ProjectItem interface represents a Visual Studio project item. Visual Studio passes a reference to the ProjectItem interface object that represents the item that a template adds to a project in the ProjectItemFinishedGenerating method. It also passes a reference to ProjectItem objects representing each of the items it is to open in the BeforeOpeningFile method. Finally, all of a project's items are accessible through the EnvDTE.Project object's ProjectItems property. It includes the following members:

  • ContainingProject property: Returns a reference to the EnvDTE.Project object representing the project to which this item belongs.
  • FileCodeModel property: Returns an EnvDTE.FileCodeModel object representing the item's source code. By working with this portion of the object model, a wizard can access, modify, or add to the item's source code.
  • FileCount property: Returns the number of files associated with this project item.
  • FileNames property: Returns the path and names of files associated with this project item.
  • Name property: The name of the project.
  • Properties property: Returns a Properties collection object that provides access to project item's properties. Each member of the collection is represented by an EnvDTE.Property object. Some of the useful properties found in this collection are listed in Table 2.
  • Table 2. Selected Property objects in the ProjectItem object's Properties collection.

    Name Type Description
    Extension String The file extension of the project item.
    Filename String The root filename of the project item.
    FullPath String The fully qualified path of the project item's file.

  • Saved property: Indicates whether the item has not been modified since it was last saved or opened.
  • Delete method: Removes the item from the project and deletes it from the file system.
  • Open method: Opens the item in a window in the Visual Studio development environment.
  • Remove method: Removes the item from the project.
  • Save method: Saves the current project or a designated item in the project.
  • SaveAs method: Saves the current project or a designated item in the project using a specified filename.

The VSProject Interface Object

The VSProject interface, which is new to the Visual Studio 2005 extensibility model, represents a Visual Basic, Visual C#, or Visual J# project in the Visual Studio environment. It provides a project-level object model that you might otherwise expect to find in the EnvDTE.Project interface. The VSProject object and its child objects are defined in the VSLangProj namespace and require that a reference to the valangproj.dll assembly be added to your wizard development project.

To instantiate a VSProject object, you convert or cast the EnvDTE.Project object passed to the ProjectFinishedGenerating method to a VSProject object. This requires a C# statement like the following in the code for the ProjectFinishedGenerating method:

VSProject vsproject = (VSProject) project.Object;

The equivalent Visual Basic statement is:

Dim vsproject As VSProject = DirectCast(project.Object, VSProject)

The VSProject object includes the following members:

  • Imports property: In Visual Basic projects, returns an Imports interface object containing a collection of strings that define project-wide imported namespaces. (Since C# does not support project-wide imported namespaces, this property returns null.) You can then enumerate the namespaces in the collection, remove individual namespaces, or add additional imported namespaces.

  • References property: Returns a References interface object representing the references added to the project. Using the References collection, your wizard can enumerate references, find a particular reference, remove existing references, or add new references.
  • TemplatePath property: The path to (but not the filename of) the template file that has caused the wizard to execute.
  • Refresh method: Refreshes the Visual Studio Solution Explorer. The wizard can call this method if it adds files to a project programmatically or adds or removes project references.

Handling Abnormal Termination

Particularly if your wizard displays a user interface, you have to be prepared for the possibility that the user might cancel your wizard or fail to provide some information necessary for the wizard to continue. In this case, you want Visual Studio to gracefully exit from the wizard, rather than to throw an exception to the wizard user.

Interestingly, the wizard communicates with Visual Studio by throwing either of two exceptions, both of which are defined in the Microsoft.VisualStudio.TemplateWizard namespace. (They're found in the Microsoft.VisualStudio.TemplateWizardInterface.dll assembly, the same assembly that contains the IWizard interface.) The first, WizardCancelledException, indicates that the wizard has been cancelled before it is completed. The second, WizardBackoutException, indicates that the wizard is to be backed out before it has been completed. These exceptions are unusual insofar as your wizard throws the exception, and Visual Studio itself provides the exception handling code that handles the exception.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow