Share via


방법: 프로그래밍 방식으로 프로젝트 항목 만들기

Visual Studio의 추가 기능은 Visual Studio 2013에서 사용되지 않습니다. 추가 기능을 VSPackage 확장으로 업그레이드하는 것이 좋습니다. 업그레이드에 대한 자세한 내용은 FAQ: VSPackage 확장으로 추가 기능 변환 을 참조하십시오.

프로그래밍 방식으로 프로젝트 항목을 만들려면 먼저 GetProjectItemTemplate을 호출한 다음 반환된 템플릿 경로를 AddFromTemplate에 전달합니다. 자세한 내용은 Visual Studio Templates을 참조하십시오.

GetProjectItemTemplate 메서드는 AddFromTemplate 메서드에 사용할 템플릿을 해당 .zip 파일에서 반환합니다. 모든 언어에 대한 프로젝트 항목 템플릿은 Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\Language\에 있습니다.

사용자 지정 프로젝트 항목 템플릿을 만들 수도 있습니다. 템플릿을 저장할 디렉터리를 지정하려면 도구 메뉴에서 옵션을 클릭합니다. 옵션 대화 상자의 왼쪽 창에서 프로젝트 및 솔루션을 클릭합니다. Visual Studio 사용자 항목 템플릿 위치 상자에 사용자 템플릿의 경로를 입력합니다.

사용자 지정 템플릿은 Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\Language\에 정의된 파일 이름과 충돌하지 않는 고유한 파일 이름을 가져야 합니다.

8.3 형식이 아닌 긴 파일 이름을 사용해야 합니다. 자세한 내용은 Creating Project and Item Templates을 참조하십시오.

솔루션에서 프로젝트를 제거하려면 Remove를 사용합니다.

다음 예제에서는 제네릭 메서드를 사용하여 프로젝트 항목을 만듭니다. 참고 항목에 나열된 항목에서는 언어별 모델을 사용하는 방법을 보여 줍니다.

참고

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

프로젝트에 항목 추가

프로그래밍 방식으로 프로젝트에 항목을 추가하려면

  1. Visual Studio를 시작하고 Visual Studio 추가 기능 프로젝트를 만듭니다.

  2. 추가 기능의 Connect 클래스에 이 항목의 뒤에 나오는 코드를 추가합니다.

  3. 추가 기능 프로젝트를 실행한 후 도구 메뉴에서 추가 기능 관리자를 클릭하고 추가 기능 옆에 있는 상자를 선택하여 추가 기능 관리자에서 활성화합니다.

예제

다음 예제에서는 기존 Visual Basic 프로젝트에 프로그래밍 방식으로 항목을 추가하는 방법을 보여 줍니다.

' Before running the following code, be sure that a Visual Basic 
' project is open in Visual Studio.
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)
    createProjectItem(_applicationObject)
End Sub

Sub createProjectItem(ByVal dte As DTE2)
    ' Adds a new Class to an existing Visual Basic project.
    Dim soln As Solution2
    Dim prj As Project
    soln = CType(_applicationObject.Solution, Solution2)
    Dim prjItem As ProjectItem
    Dim itemPath As String

    ' Point to the first project (the Visual Basic project).
    prj = soln.Projects.Item(1)
    ' Retrieve the path to the Class template.
    itemPath = soln.GetProjectItemTemplate("Class.zip", "vbproj")
    ' Create a new project item based on the template, in this case,
    ' a Class.
    prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass")
End Sub
// Before running the following code, be sure that a Visual Basic 
// project is open in Visual Studio.
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst, ref
 System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    // Pass the applicationObject member variable to the code example.
    createProjectItem(_applicationObject);
}
public void createProjectItem(DTE2 dte)
{
    //Adds a new Class to an existing Visual Basic project.
    Solution2 soln;
    Project prj;
    soln = (Solution2)_applicationObject.Solution;
    ProjectItem prjItem;
    String itemPath;
    // Point to the first project (the Visual Basic project).
    prj = soln.Projects.Item(1);
    // Retrieve the path to the class template.
    itemPath = soln.GetProjectItemTemplate("Class.zip", "vbproj");
    //Create a new project item based on the template, in this
    // case, a Class.
    prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass");
}

코드 컴파일

이 코드를 컴파일하려면 Visual Studio 추가 기능 프로젝트를 만들고 Connect.cs 또는 Connect.vb 클래스의 코드를 예제에 있는 코드로 변경합니다. 추가 기능을 실행하기 전에 Visual Studio에서 Visual Basic 프로젝트를 엽니다. 추가 기능을 실행하는 방법에 대한 내용은 방법: 추가 기능 관리자를 사용하여 추가 기능 제어를 참조하십시오.

강력한 프로그래밍

프로젝트 항목 이름을 Solution.Projects.Item에 대한 매개 변수로 사용하는 경우 프로젝트의 고유 이름을 사용해야 합니다. 여기서 고유 이름이란 솔루션 파일(.sln)을 포함하는 디렉터리에서 프로젝트 파일로의 상대 경로를 나타냅니다.

예를 들어 다음과 같은 솔루션/프로젝트 구조를 가정해 봅니다.

SomeSolution.sln

     WinApp1

          WinApp1.VBProj

프로젝트의 고유 이름은 "WinApp1/WinApp1.VBProj"가 되며 Item 메서드에 대한 호출은 Solution.Projects.Item("WinApp1/WinApp1.VBProj")과 같습니다.

참고 항목

작업

방법: 자동화 개체 모델 코드의 예제 컴파일 및 실행

방법: 프로그래밍 방식으로 프로젝트 만들기

개념

Visual Basic 및 Visual C# 프로젝트 조작

Visual C++ 프로젝트 조작

Visual Studio 템플릿 소개

기타 리소스

솔루션 및 해당 프로젝트 제어