如何:以编程方式创建项目项

Visual Studio 2013 中已弃用 Visual Studio 的外接程序。 你应该升级外接程序到 VS 的扩展包。 有关升级的更多信息,请参见 。常见问题:将外接程序转换为 VSPackage 扩展

若要以编程方式创建项目项,请首先调用 GetProjectItemTemplate,然后将返回的模板路径传递给 AddFromTemplate。 有关详细信息,请参阅Visual Studio Templates

GetProjectItemTemplate 方法返回适当的 .zip 文件中的模板,以与 AddFromTemplate 方法一起使用。 在 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 模板介绍

其他资源

控制解决方案及其项目