2015-04-05 47 views

回答

1

这不是很直接,但有一个有趣的指南MSDN解释如何做到这一点。它适用于加载项,但在VSPackage中,您拥有相同的Visual Studio DTE对象集(DTE应用程序)。

您可以定义一个使用GetProjectTemplate和AddFromTemplate创建两个控制台项目的方法。您可以在VSPackage的类OLE菜单命令的方法Initialize定义(如果这是你在找什么):

protected override void Initialize() 
{ 
    //// Create the command for the menu item. 
    var aCommand = new CommandID(GuidList.GuidCmdSet, (int)PkgCmdIdList.CmdId); 
    var menuItemEnable = new OleMenuCommand((s, e) => createProjectsFromTemplates(), aCommand); 
} 

,然后定义(在这种情况下createProjectsFromTemplates)相关的命令的方法创建一个项目的解决方案:

private DTE2 _mApplicationObject; 

    public DTE2 ApplicationObject 
    { 
     get 
     { 
      if (_mApplicationObject != null) return _mApplicationObject; 
      // Get an instance of the currently running Visual Studio IDE 
      var dte = (DTE)GetService(typeof(DTE)); 
      _mApplicationObject = dte as DTE2; 
      return _mApplicationObject; 
     } 
    } 

public void createProjectsFromTemplates() 
{ 
    try 
    { 
     // Create a solution with two projects in it, based on project 
     // templates. 
     Solution2 soln = (Solution2)ApplicationObject.Solution; 
     string csTemplatePath; 

     string csPrjPath = "C:\\UserFiles\\user1\\addins\\MyCSProject"; 
     // Get the project template path for a C# console project. 
     // Console Application is the template name that appears in 
     // the right pane. "CSharp" is the Language(vstemplate) as seen 
     // in the registry. 
     csTemplatePath = soln.GetProjectTemplate(@"Windows\ClassLibrary\ClassLibrary.vstemplate", 
      "CSharp"); 
     System.Windows.Forms.MessageBox.Show("C# template path: " + 
      csTemplatePath); 
      // Create a new C# console project using the template obtained 
     // above. 
     soln.AddFromTemplate(csTemplatePath, csPrjPath, "New CSharp 
      Console Project", false); 

    } 
    catch (System.Exception ex) 
    { 
     System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message); 
    } 
} 

对于10.0以后的Visual Studio版本,模板项目的zip不再可用。该.vstemplate必须引用,就可以找到该文件夹​​下的所有项目模板:这个MSDN link

C:\Program Files (x86)\Microsoft Visual Studio 1x.0\Common7\IDE\ProjectTemplates\ 

更多信息。

该方法应该创建一个基于C#项目模板(例如包含class1.cs作为初始文件)的C#项目的解决方案。

如果您希望并根据该自定义模板创建解决方案,您也可以定义自己的模板。以下是关于如何创建自定义模板的MSDN的指南。

希望它有帮助。

+0

非常有见地,谢谢。但是,您如何知道为模板编写“ConsoleApplication.zip”?你在哪里看到这个?在什么右窗格中? – Darius 2015-04-06 07:06:17

+0

我编辑了我的答案,查看我的更改 – codingadventures 2015-04-06 13:25:55

相关问题