如何:访问特定类型的项目中的文件的属性

您可以在 Visual Studio 集成开发环境 (IDE) 中手动设置和检查项目中文件的文件属性。 若要检查文件属性,请在 Visual Studio 中打开一个项目,并在**“解决方案资源管理器”中右击某个项目文件,如 filename.cs。 从快捷菜单中选择“属性”,以显示“属性”**对话框。 **“属性”**对话框显示可以为所选文件手动设置的文件属性。

VSLangProj80 命名空间提供了一种方法,可通过编程方式访问 Visual C# 或 Visual Basic 项目中的文件属性。 具体说来,FileProperties2 定义了一组丰富的属性,用于控制和访问文件信息。 在 FileProperties2 中定义的某些属性并非对每种文件类型都有效。 例如,DateCreated 属性是为代码文件定义的,不适用于其他项目文件。

若要访问特定的 FileProperties2 属性,必须将特定的属性名称作为字符串传递给 EnvDTE.Property.Properties.Item(object index),如下面的代码示例所示。

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");

此代码访问 Visual C# 或 Visual Basic 项目内某个文件的 FileName 属性。

实际上,在 FileProperties2 中定义的属性是可作为 Visual C# 或 Visual Basic 项目的项目属性项进行访问的文件的可用属性引用列表。

下面的步骤详细说明了如何在 Visual Studio 外接程序中以编程方式访问文件属性。

备注

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

访问特定项目类型的文件的属性

  1. 使用 Visual C# 创建 Visual Studio 外接程序项目。

  2. 在**“项目”菜单上,单击“添加引用”,再单击“.NET”选项卡,选择“VSLangProj”“VSLangProj2”“VSLangProj80”,然后单击“确定”**。

  3. 将以下 using 语句添加到 Connect.cs 文件顶部。

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    
  4. 将下面的方法调用添加到 OnConnection 方法。

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        VSProjectFileProps2(_applicationObject);
    }
    
  5. 添加 VSProjectFileProps2 方法,使其紧跟在 OnConnection 方法的下方。

    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);
        }
    }
    

    VSProjectFileProps2 列出了项目中每个文件的 FileName 属性。 随后此方法确定文件是否具有 .cs 或 .vb 扩展名。 如果有,则会显示 FilesizeDateCreated 属性值。

    示例部分列出了完整的代码

  6. 通过单击**“生成”菜单上的“生成解决方案”**生成外接程序。

  7. 在 Visual Studio IDE 中打开一个 Visual C# 或 Visual Basic 项目。

  8. 在**“工具”菜单上,单击“外接程序管理器”,并从“外接程序管理器”对话框中选择您的外接程序。 单击“确定”**以运行外接程序。

示例

下面的示例是一个简单的 Visual Studio 外接程序,它演示如何通过使用 Visual Studio 中的自动化功能访问特定类型的项目中的文件属性。

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

编译代码

若要编译此代码,请创建一个新的 Visual Studio 外接程序项目,然后用本示例中的代码替换 OnConnection 方法的代码。 有关如何运行外接程序的信息,请参见如何:使用外接程序管理器控制外接程序

请参见

其他资源

Project Properties

访问特定于项目类型的项目、项目项和配置属性