다음을 통해 공유


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

업데이트: 2007년 11월

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

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

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

사용자 지정 템플릿의 파일 이름은 다음 위치에 정의되어 있는 파일 이름과 충돌하지 않는 고유한 것이어야 합니다. <drive>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\Language.

8.3 형식이 아닌 긴 파일 이름을 사용해야 합니다. 자세한 내용은 프로젝트 템플릿 및 항목 템플릿 만들기를 참조하십시오.

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

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

참고:

표시되는 대화 상자와 메뉴 명령은 실제 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다. 이러한 절차는 일반 개발 설정을 사용하여 개발되었습니다. 설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다. 자세한 내용은 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 IDE에서 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 템플릿 소개

기타 리소스

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