如何:操作 Visual Basic 项目的 Imports 属性

大多数 VSProject2 方法和属性都适用于 Visual C# 和 Visual Basic 项目。 有关更多信息,请参见如何:使用 VSProject2 对象操作 Visual Basic 和 C# 项目VSProject2 对象的 Imports 属性特定于 Visual Basic 项目。 它通过使用添加和枚举 Imports 集合的方法,提供对 Imports 对象的访问。

下面的步骤解释如何使用 Visual Studio 外接程序,以编程方式控制 Visual Basic 项目中的 Imports 属性。

提示

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

使用 VSProject2 对象控制 Visual Basic 项目

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

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

  3. 在计算机上创建一个文件夹:

    • <安装根目录>\UserFiles\MyProjects\MyTestProject

      在此示例中,<安装根目录> 为“C:”。

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

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using VSLangProj90;
    
  5. 使用 VSLangProj100;将以下方法调用添加到 OnConnection 方法。

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

    public void CSVSProj2Manip(DTE2 dte)
    {
    }
    
  7. 将下面的声明添加到此方法的顶部。

    Solution2 soln = (Solution2)_applicationObject.Solution;
    String vbTemplatePath;
    String vbPrjPath;
    Project proj;
    VSProject2 vsproj;
    Imports impCollection;
    
  8. 使用 AddFromTemplate 创建一个 Visual C# 项目。

    • 获取模板的语法为 EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "VisualBasic"),其中的名称 "WindowsApplication.zip" 从位于 <安装根目录>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\2052 文件夹中的 WindowsApplication.zip 文件获得。 对于所有 Visual Studio 项目类型,都可以在 <安装根目录>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\语言 文件夹中找到这些文件。 "VisualBasic" 指定此项目是一个 Visual Basic项目。
    // Make sure you create the folders that 
    // make up the file path
    // on your computer. You can replace 
    // this with your own file path.
    vbPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject";
    // Get the project template path for a C# windows 
    // application.
    vbTemplatePath = soln.GetProjectTemplate
    ("WindowsApplication.zip", "VisualBasic");
            // Create a new Windows application by using the 
    // template obtained above.
    soln.AddFromTemplate(vbTemplatePath, vbPrjPath,
     "Test2VBProj", false);
    
  9. 添加下面的代码,以演示如何使用通过 Imports 属性获取的 Imports

    proj = soln.Projects.Item(1);
    // Get a reference to the VSProject2 object.
    vsproj = (VSProject2)proj.Object;
    // Add a reference to System.Security.dll.
    MessageBox.Show("Adding a reference to System.Security.dll");
    // Remove the <version number> in the following path
    // and replace it with one of the version 
    // number folders that appear 
    // in <installation root>\WINDOWS\Microsoft.NET\Framework
    // folder
    vsproj.References.Add
    ("C:\\WINDOWS\\Microsoft.NET\\Framework\\
    <version number>\\System.Security.dll");
    impCollection = vsproj.Imports;
    MessageBox.Show("The number of imports in this project is: " 
    + impCollection.Count.ToString() + "\n");
    MessageBox.Show
    ("Adding System.Security to the Imports collection.");
    impCollection.Add("System.Security");
    MessageBox.Show("The number of imports in this project is now: " 
    + impCollection.Count.ToString() + "\n");
    String temp = null;
    for (int i = 1; i <= impCollection.Count; i++)
    {
        temp = temp + impCollection.Item(i).ToString() + "\n";
    }
    MessageBox.Show("The Imports in this project are:" + "\n" + temp);
    

    VBVSProj2Manip 方法使用 VSProject2 对象执行以下操作:

    Imports 上的方法用于执行以下操作:

    • 使用 Add 将 System.Security 添加到 Imports 集合。

    • 使用 Count 属性显示 Imports 集合中项的数目。

    • 使用 Item 方法显示 Imports 集合中项的名称。

    示例部分列出了完整的代码,其中包括整个方法的 try-catch 块。

  10. 若要生成外接程序,请单击**“生成”菜单上的“生成解决方案”**。

  11. 打开 Visual Studio 集成开发环境 (IDE) 中的一个 Visual Basic 项目。

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

示例

下面的示例是一个基本 Visual Studio 外接程序,演示如何通过使用 Visual Studio 自动化来使用 Imports 属性。

using System;
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using VSLangProj90;
using VSLangProj100;
public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    VBVSProj2Manip(_applicationObject);
}
public void VBVSProj2Manip(DTE2 dte)
{
    try
    {
        Solution2 soln = (Solution2)_applicationObject.Solution;
        String vbTemplatePath;
        String vbPrjPath;
        Project proj;
        VSProject2 vsproj;
        Imports impCollection;
        // Make sure you create the folders that make up the file path
        // on your computer. You can replace this with 
        // your own file path.
        vbPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject";
        // Get the project template path for a Visual Basic windows
        // application.
        vbTemplatePath = soln.GetProjectTemplate
("WindowsApplication.zip", "VisualBasic");
        // Create a new Windows application by using the 
        // template obtained above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath,
 "Test2VBProj", false);
        proj = soln.Projects.Item(1);
        // Cast to the VSProject2 object.
        vsproj = (VSProject2)proj.Object;
        // Add a reference to System.Security.dll.
        MessageBox.Show("Adding a reference to System.Security.dll");
        // Remove the <version number> in the following path
        // and replace it with one of the version 
        // number folders that appear 
        // in <installation root>\WINDOWS\Microsoft.NET\Framework
        // folder
        vsproj.References.Add
("C:\\WINDOWS\\Microsoft.NET\\Framework\\
<version number>\\System.Security.dll");
        vsproj.Refresh();
        impCollection = vsproj.Imports;
        MessageBox.Show("The number of imports in this project is: " 
+ impCollection.Count.ToString() + "\n");
        MessageBox.Show("Adding System.Security to the 
Imports collection.");
        impCollection.Add("System.Security");
        MessageBox.Show("The number of imports in this project is now:
 " + impCollection.Count.ToString() + "\n");
        String temp = null;
        for (int i = 1; i <= impCollection.Count; i++)
        {
            temp = temp + impCollection.Item(i).ToString() + "\n";
        }
        MessageBox.Show("The Imports in this project are:" + "\n" 
+ temp);
    }
    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
Imports VSLangProj90
Imports VSLangProj100
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)
    VBVSProj2Manip(_applicationObject)
End Sub
Sub VBVSProj2Manip(ByVal dte As DTE2)
    Try
        Dim soln As Solution2 = CType(_applicationObject.Solution, _
        Solution2)
        Dim vbTemplatePath As String
        Dim vbPrjPath As String
        Dim proj As Project
        Dim vsproj As VSProject2
        Dim impCollection As [Imports]
        ' Create this or your own file path on your computer.
        ' The file path needs to exist before you run this add-in.
        vbPrjPath = "C:\UserFiles\MyProjects\MyTestProject"
        ' Get the project template path for a Visual Basic 
        ' Windows application.
        vbTemplatePath = soln.GetProjectTemplate _
         ("WindowsApplication.zip", "VisualBasic")
         ' Create a new Windows Application by using the 
        ' template obtained above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath, _
        "Test2JSProj", False)
        proj = soln.Projects.Item(1)
        ' Cast the project to a VSProject2.
        vsproj = CType(proj.Object, VSProject2)
        ' Add a reference to System.Security.dll.
        MsgBox("Adding a reference to System.Security.dll")
        ' Remove the <version number> in the following path
        ' and replace it with one of the version 
        ' number folders that appear 
        ' in <installation root>\WINDOWS\Microsoft.NET\Framework
        ' folder
        vsproj.References.Add _
        ("C:\WINDOWS\Microsoft.NET\Framework\ _
        <version number>\System.Security.dll")
        impCollection = vsproj.Imports
        MsgBox("The number of imports in this project is: " & vbCr _
        & impCollection.Count.ToString())
        MsgBox("Adding System.Security to the Imports collection.")
        impCollection.Add("System.Security")
        MsgBox("The number of imports in this project is now: "  _
        & vbCr & impCollection.Count.ToString())
        Dim temp As String = ""
        For i As Integer = 1 To impCollection.Count
            temp = temp & impCollection.Item(i).ToString() & vbCr
        Next i
        MsgBox("The Imports in this project are:" & vbCr & temp)
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub

编译代码

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

请参见

概念

VSProject2 对象介绍

其他资源

扩展 Visual Basic 和 Visual C# 项目