Compartir a través de


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

You can manually set and examine file properties of files in the project in the Visual Studio integrated development environment (IDE). To examine file properties, open a project in Visual Studio, right-click a project file, such as filename.cs, in Solution Explorer. From the shortcut menu, select Properties to display the Properties dialog box. The Properties dialog box displays the file properties you can manually set for the file you chose.

The VSLangProj80 namespace offers a way to programmatically access file properties in Visual C# or Visual Basic projects. Specifically, FileProperties2 defines a rich set of properties for controlling and accessing file information. Some properties defined in FileProperties2 are not valid for each type of file. For example, the DateCreated property is defined for code files but not for other project files.

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

Project project;
ProjectItems projItems;
ProjectItem projItem;
Property prop;
project = _applicationObject.Solution.Projects.Item(1);
projItems = project.ProjectItems;
projItem = projItems.Item(1);
prop = projItem.Properties.Item("FileName");

This code accesses the FileName property of a file within a Visual C# or Visual Basic project.

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

The steps below detail how to access the file 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 files 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;
        VSProjectFileProps2(_applicationObject);
    }
    
  5. Add the VSProjectFileProps2 method immediately below the OnConnection method.

    public void VSProjectFileProps2(DTE2 dte)
    {
        try
        {
            // Open a Visual C# or Visual Basic project
            // before running this add-in.
            Project project;
            ProjectItems projItems;
            ProjectItem projItem;
            Property prop;
            project = _applicationObject.Solution.Projects.Item(1);
            projItems = project.ProjectItems;
            for(int i = 1 ; i <= projItems.Count; i++ )
            {
                projItem = projItems.Item(i);
                prop = projItem.Properties.Item("FileName");
                MessageBox.Show("The file name of item " + i + " is: " 
    + prop.Value.ToString());
                if (prop.Value.ToString().Contains(".cs") 
    || prop.Value.ToString().Contains(".vb"))
                {
                    prop = projItem.Properties.Item("FileSize");
                    MessageBox.Show("The file size of item " + i + " 
    is: " + prop.Value.ToString());
                    prop = projItem.Properties.Item("DateCreated");
                    MessageBox.Show("The creation date of item " + i 
    + " is: " + prop.Value.ToString());
                }
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    The VSProjectFileProps2 lists the FileName property for each file in the project. The method then determines if the file has a .cs or .vb extension. If it does, the Filesize and DateCreated property values are also displayed.

    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.

Example

The following example is a basic Visual Studio add-in that demonstrates how to access properties of a file 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;
    VSProjectFileProps2(_applicationObject);
}
public void VSProjectFileProps2(DTE2 dte)
{
    try
    {
        // Open a Visual C# or Visual Basic project
        // before running this add-in.
        Project project;
        ProjectItems projItems;
        ProjectItem projItem;
        Property prop;
        project = _applicationObject.Solution.Projects.Item(1);
        projItems = project.ProjectItems;
        for(int i = 1 ; i <= projItems.Count; i++ )
        {
            projItem = projItems.Item(i);
            prop = projItem.Properties.Item("FileName");
            MessageBox.Show("The file name of item " + i + " is: " 
+ prop.Value.ToString());
            if (prop.Value.ToString().Contains(".cs") 
|| prop.Value.ToString().Contains(".vb"))
            {
                prop = projItem.Properties.Item("FileSize");
                MessageBox.Show("The file size of item " + i + " is: "
 + prop.Value.ToString());
                prop = projItem.Properties.Item("DateCreated");
                MessageBox.Show("The creation date of item " + i 
+ " is: " + 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)
    VSProjectFileProperties2(_applicationObject)
End Sub
Sub VSProjectFileProperties2(ByVal dte As DTE2)
    ' Open a Visual C# or Visual Basic project
    ' before running this add-in.
    Try
        Dim project As Project
        Dim projItems As ProjectItems
        Dim projItem As ProjectItem
        Dim prop As [Property]
        project = _applicationObject.Solution.Projects.Item(1)
        projItems = project.ProjectItems
        For i As Integer = 1 To projItems.Count
            projItem = projItems.Item(i)
            prop = projItem.Properties.Item("FileName")
            MsgBox("The file name of item " & i & " is: "  _
            & prop.Value.ToString())
            If (prop.Value.ToString().Contains(".cs")  _
            Or prop.Value.ToString().Contains(".vb")) Then
                prop = projItem.Properties.Item("FileSize")
                MsgBox("The file size of item " & i & " is: "  _
                & prop.Value.ToString())
                prop = projItem.Properties.Item("DateCreated")
                MsgBox("The creation date of item " & i & " is: "  _
                & prop.Value.ToString())
            End If
        Next i
        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