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.

NoteNote

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.

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.

No code example is currently available or this language may not be supported.

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.

Show: