2015-04-05 72 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

相關問題