共用方式為


HOW TO:存取特定專案類型的檔案屬性

您可以在 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 增益集 (Add-In) 中以程式設計方式存取檔案屬性。

注意事項注意事項

根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。 使用 [一般開發設定] 開發了這些程序。 若要變更設定,請從 [工具] 功能表中選取 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

若要存取特定專案類型的檔案屬性

  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. 緊接在 OnConnection 方法的下面加入 VSProjectFileProps2 方法。

    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 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

編譯程式碼

若要編譯這個程式碼,請建立新的 Visual Studio 增益集專案,並以範例中的程式碼取代 OnConnection 方法的程式碼。 如需如何執行增益集的詳細資訊,請參閱 HOW TO:使用增益集管理員來控制增益集

請參閱

概念

專案屬性

其他資源

存取專案類型特定專案、專案項目和組態屬性