HOW TO:存取特定的專案類型的資料夾屬性

更新:2007 年 11 月

如果您在 Visual Studio 整合式開發環境 (IDE) 中開啟專案,並以滑鼠右鍵按一下 [方案總管] 中的資料夾,就可以用手動方式設定及檢視資料夾屬性。在快速鍵功能表上按一下 [屬性],以顯示 [屬性] 對話方塊。

VSLangProj80 命名空間 (Namespace) 可以讓您以程式設計方式存取 Visual C#、Visual J# 或 Visual Basic 專案中的資料夾屬性。特別是,FolderProperties2 會定義一組非常豐富的屬性,可以用來控制及存取資料夾資訊。許多在 FolderProperties2 中定義的屬性都無法以手動方式從 [屬性] 視窗中存取。

若要存取特定的 FolderProperties2 屬性,必須將特定的屬性名稱當做字串 (String) 傳遞至 EnvDTE.Property.Properties.Item(object index),如下列程式碼範例中所示:

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

這個程式碼會在 Visual C#、Visual J# 或 Visual Basic 專案中存取資料夾的 FullPath 屬性。

實際上,在 FolderProperties2 中定義的屬性可以當做一份參考清單,裡面包含了 Visual C#、Visual J# 或 Visual Basic 專案中可以當做專案屬性項目存取之所有可使用的資料夾屬性。

在下列步驟中,會詳細說明如何在 Visual Studio 增益集 (Add-In) 中以程式設計方式存取這些屬性。

注意事項:

根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 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;
        VSProjectFolderProps2(_applicationObject);
    }
    
  5. 緊接在 OnConnection 方法的下面加入 VSProjectFolderProps2 方法。

    public void VSProjectFolderProps2(DTE2 dte)
    {
        try
        {
            // Open a Visual C#, Visual J#, 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);
        }
    }
    

    範例部分會列出完整的程式碼。

  6. 按一下 [建置] 功能表上的 [建置方案],建置增益集。

  7. 在 Visual Studio IDE 中,開啟 [Visual C#]、[Visual J#] 或 [Visual Basic] 專案。

  8. 按一下 [工具] 功能表上的 [增益集管理員],然後從 [增益集管理員] 對話方塊中選取增益集。按一下 [確定],執行您的增益集。

    FullPathFileNameURL 的資料夾屬性便會顯示在訊息方塊中。

範例

下列是基本 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;
    VSProjectFolderProps2(_applicationObject);
}
public void VSProjectFolderProps2(DTE2 dte)
{
    try
    {
        // Open a Visual C#, Visual J#, 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#, Visual J#, 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

編譯程式碼

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

請參閱

概念

專案屬性

其他資源

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