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

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

VSLangProj80 命名空间提供了一种方法,可通过编程方式访问 Visual C# 或 Visual Basic 项目中的文件夹属性。 具体来说,FolderProperties2 定义了一组齐全的属性,用于控制和访问文件夹信息。 FolderProperties2 中定义的许多属性无法从**“属性”**窗口手动访问。

若要访问特定的 FolderProperties2 属性,必须将特定的属性名称作为字符串传递给 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 Basic 项目内某个文件夹的 FullPath 属性。

实际上,在 FolderProperties2 中定义的属性是可作为 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;
        VSProjectFolderProps2(_applicationObject);
    }
    
  5. 将 VSProjectFolderProps2 方法添加到 OnConnection 方法的正下方。

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

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

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

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

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

    消息框中显示 FullPathFileNameURL 的文件夹属性。

示例

下面的示例是一个基础 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;
    VSProjectFolderProps2(_applicationObject);
}
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);
    }
}
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# 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 方法的代码。 有关如何运行外接程序的信息,请参见如何:使用外接程序管理器控制外接程序

请参见

其他资源

Project Properties

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