Partager via


How to: Access Properties of Folders for Specific Types of Projects

You can manually set and examine folder properties if you open a project in the Visual Studio integrated development environment (IDE) and right-click a folder in Solution Explorer. On the shortcut menu, click Properties to display the Properties dialog box.

The VSLangProj80 namespace offers a way to programmatically access folder properties in Visual C# or Visual Basic projects. Specifically, FolderProperties2 defines a rich set of properties for controlling and accessing folder information. Many of the properties defined in FolderProperties2 cannot be accessed manually from the Properties window.

To access a specific FolderProperties2 property, you must pass the specific property name as a string to EnvDTE.Property.Properties.Item(object index), as shown in the code example below.

Project project;
ProjectItem folder;
Properties folderProps;
Property prop;
project = _applicationObject.Solution.Projects.Item(1);
folder = project.ProjectItems.AddFolder("MyFolder"
,Constants.vsProjectItemKindPhysicalFolder);
folderProps = folder.Properties;
prop = folderProps.Item("FullPath");

This code accesses the FullPath property of a folder within a Visual C# or Visual Basic project.

In effect, the properties defined in FolderProperties2 are a reference list of available properties of folders that can be accessed as property items of projects for Visual C# or Visual Basic.

The steps below detail how to access these properties programmatically in a Visual Studio add-in.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To access properties of folders for a specific type of project

  1. Create a Visual Studio add-in project by using Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select VSLangProj, VSLangProj2, and VSLangProj80, and click OK.

  3. Add the following using statements to the top of the Connect.cs file.

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    
  4. Add the following method call to the OnConnection method.

    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        VSProjectFolderProps2(_applicationObject);
    }
    
  5. Add the VSProjectFolderProps2 method immediately below the OnConnection method.

    public void VSProjectFolderProps2(DTE2 dte)
    {
        try
        {
            // Open a Visual C# or Visual Basic project
            // before running this add-in.
            Project project;
            ProjectItem folder;
            Properties folderProps;
            Property prop;
            project = _applicationObject.Solution.Projects.Item(1);
            // Add a new folder to the project.
            MessageBox.Show("Adding a new folder to the project.");
            folder =
     project.ProjectItems.AddFolder("MyFolder",
    Constants.vsProjectItemKindPhysicalFolder);
            folderProps = folder.Properties;
            prop = folderProps.Item("FullPath");
            MessageBox.Show("The full path of the new folder is:" 
    + "\n" + prop.Value.ToString());
            prop = folderProps.Item("FileName");
            MessageBox.Show("The file name of the new folder is:" 
    + "\n" + prop.Value.ToString());
            prop = folderProps.Item("URL");
            MessageBox.Show("The new folder has the following URL:" 
    + "\n" + prop.Value.ToString());
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    The example section lists the complete code.

  6. Build the add-in by clicking Build Solution on the Build menu.

  7. Open a Visual C# or Visual Basic project in the Visual Studio IDE.

  8. On the Tools menu, click Add-in Manager, and select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

    The folder properties for FullPath, FileName, and URL are displayed in message boxes.

Example

The following example is a basic Visual Studio add-in that demonstrates how access properties of a folder in a specific type of project by using Visual Studio automation.

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    VSProjectFolderProps2(_applicationObject);
}
public void VSProjectFolderProps2(DTE2 dte)
{
    try
    {
        // Open a Visual C# or Visual Basic project
        // before running this add-in.
        Project project;
        ProjectItem folder;
        Properties folderProps;
        Property prop;
        project = _applicationObject.Solution.Projects.Item(1);
        // Add a new folder to the project.
        MessageBox.Show("Adding a new folder to the project.");
        folder =
 project.ProjectItems.AddFolder("MyFolder"
,Constants.vsProjectItemKindPhysicalFolder);
        folderProps = folder.Properties;
        prop = folderProps.Item("FullPath");
        MessageBox.Show("The full path of the new folder is:" + "\n" 
+ prop.Value.ToString());
        prop = folderProps.Item("FileName");
        MessageBox.Show("The file name of the new folder is:" + "\n" 
+ prop.Value.ToString());
        prop = folderProps.Item("URL");
        MessageBox.Show("The new folder has the following URL:" 
+ "\n" + prop.Value.ToString());
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80
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)
    VSProjectConfigProperties(_applicationObject)
End Sub
Sub VSProjectConfigProperties(ByVal dte As DTE2)
    ' Open a Visual C# or Visual Basic project
    ' before running this add-in.
    Try
        Dim project As Project
        Dim folder As ProjectItem
        Dim folderProps As Properties
        Dim prop As [Property]
        project = _applicationObject.Solution.Projects.Item(1)
        ' Add a new folder to the project.
        MsgBox("Adding a new folder to the project...")
        folder = project.ProjectItems.AddFolder("MyFolder" _
        , Constants.vsProjectItemKindPhysicalFolder)
        folderProps = folder.Properties
        prop = folderProps.Item("FullPath")
        MsgBox("The full path of the new folder is:" & vbCr _
        & prop.Value.ToString())
        prop = folderProps.Item("FileName")
        MsgBox("The file name of the new folder is:" & vbCr _
        & prop.Value.ToString())
        prop = folderProps.Item("URL")
        MsgBox("The new folder has the following URL:" & vbCr  _
        & prop.Value.ToString())
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Compiling the Code

To compile this code, create a new Visual Studio add-in project and replace the code of the OnConnection method with the code in the example. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.

See Also

Other Resources

Project Properties

Accessing Project Type Specific Project, Project Item, and Configuration Properties