Share via


방법: Visual Basic 프로젝트의 Imports 속성 조작

대부분의 VSProject2 메서드와 속성은 Visual C# 및 Visual Basic 프로젝트에 적용됩니다. 자세한 내용은 방법: VSProject2 개체를 사용하여 Visual Basic 및 C# 프로젝트 조작을 참조하십시오. VSProject2 개체의 Imports 속성은 Visual Basic 프로젝트에만 적용됩니다. 이 속성은 Imports 컬렉션을 추가하고 열거하기 위한 메서드와 더불어 Imports 개체에 대한 액세스를 제공합니다.

다음 단계에서는 Visual Studio 추가 기능을 사용하여 Visual Basic 프로젝트에서 프로그래밍 방식으로 Imports 속성을 제어하는 방법에 대해 설명합니다.

참고

일부 Visual Studio 사용자 인터페이스 요소의 경우 다음 지침에 설명된 것과 다른 이름 또는 위치가 시스템에 표시될 수 있습니다.설치한 Visual Studio 버전과 사용하는 설정에 따라 이러한 요소가 결정됩니다.자세한 내용은 Visual Studio에서 개발 설정 사용자 지정을 참조하십시오.

VSProject2 개체를 사용하여 Visual Basic 프로젝트를 제어하려면

  1. Visual C#을 사용하여 Visual Studio 추가 기능 프로젝트를 만듭니다.

  2. 프로젝트 메뉴에서 참조 추가를 클릭하고 .NET 탭을 클릭한 다음 VSLangProj, VSLangProj2 및 VSLangProj80을 선택하고 확인을 클릭합니다.

  3. 컴퓨터에 폴더를 만듭니다.

    • <Installation Root>\UserFiles\MyProjects\MyTestProject

      이 예제에서 <Installation Root>는 "C:"입니다.

  4. Connect.cs 파일의 맨 위에 다음과 같은 using 문을 추가합니다.

    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. OnConnection 메서드 바로 아래 CSVSProj2Manip 메서드 선언을 추가합니다.

    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" 이름은 <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033 폴더에 있는 WindowsApplication.zip 파일에서 가져온 것입니다. 모든 Visual Studio 프로젝트 형식에 대해 이 파일은 <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language 폴더에 들어 있습니다. "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 개체를 사용하여 다음 작업을 수행합니다.

    • References를 사용하여 System.Security.dll에 대한 참조 추가

    • Imports 속성을 사용하여 Imports에 대한 핸들 가져오기

    Imports의 메서드는 다음 작업에 사용됩니다.

    • Add를 사용하여 Imports 컬렉션에 System.Security 추가

    • 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# 프로젝트 확장