vs2010自定义项目模板

一、创建Wizard组件

1、vs2010新建类库wizard,添加引用“Microsoft.VisualStudio.TemplateWizard”、“EnvDTE”, 其中“EnvDTE”嵌入式互操作我设置为false了

2、新建类Class1, Class1继承自IWizard,实现RunStarted接口,其余为空

1 System.Windows.Forms.MessageBox.Show("12345");
2             replacementsDictionary.Add("$moni$", "yesterday");
3             replacementsDictionary.Add("$yenni$", "today");

   replacementsDictionary能添加参数,也能修改参数

3、wizard工程右键属性-》签名,选中“为程序集签名”,新建密钥文件,设置密钥.

4、打开vs命令提示符,注册到GAC

 C:\Program Files\Microsoft Visual Studio 10.0\VC>gacutil /i "D:\users\adminis tor\documents\visual studio 2010\Projects\wizard\wizard\bin\Debug\wizard.dll" 

  GAC操作命令,卸载重新注册之后,vs需重启

   注册              gacutil /i 项目名.dll  

   卸载              gacutil /u 项目名     这里不能带dll

   查看注册情况       gacutil /lr 项目名     这里不能带dll

二、创建.zip模板文件

1、准备好将作为模板的项目文件,添加好Wizard组件中用于替换的参数

2、文件菜单下导出模板,向导过程中去掉“自动导入模板到vs..”,导出为zip文件,解压后可以看到模板内容

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace $safeprojectname$
 7 {
 8     class Program
 9     {
10         // $yenni$
11         static void Main(string[] args)
12         {
13             // $moni$
14             int a = 12;
15             
16             // $haha$
17             Console.WriteLine(a);
18         }
19     }
20 }

3、修改MyTemplate.vstemplate文件, 

CustomParameters添加自定义参数,WizardExtension添加wizard组件
 1   <TemplateContent>
 2     <Project TargetFileName="t11.csproj" File="t11.csproj" ReplaceParameters="true">
 3       <ProjectItem ReplaceParameters="true" TargetFileName="Program.cs">Program.cs</ProjectItem>
 4       <Folder Name="Properties" TargetFolderName="Properties">
 5         <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
 6       </Folder>
 7     </Project>
 8     <CustomParameters>
 9       <CustomParameter Name="$haha$" Value="a=9;"/>
10     </CustomParameters>
11   </TemplateContent>
12   <WizardExtension>
13     <Assembly>wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d93913adab93889d</Assembly>
14     <FullClassName>wizard.Class1</FullClassName>
15   </WizardExtension>

4、重新压缩为zip文件,并复制到vs模板目录

C:\Users\Administrator\Documents\Visual Studio 2010\Templates\ProjectTemplates

5、重新打开vs,新建项目时可以看到新建的模板,新建后模板参数自动替换成预制值

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace tw16
 7 {
 8     class Program
 9     {
10         // today
11         static void Main(string[] args)
12         {
13             // yesterday
14             int a = 12;
15 
16             // a=9;
17             Console.WriteLine(a);
18         }
19     }
20 }

三、创建vsi文件

该文件可以双击直接安装,模板文件会自动导入到vs中,但是不会将wizard文件注册到gac中。

1、新建扩展名为.vscontent 的xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
 3   <Content>
 4     <FileName>tw.zip</FileName>
 5     <DisplayName>tw Project</DisplayName>
 6     <Description> A tw project created for this example.</Description>
 7     <FileContentType>VSTemplate</FileContentType>
 8     <ContentVersion>1.0</ContentVersion>
 9     <Attributes>
10       <Attribute name="ProjectType" value="Visual C#"/>
11       <Attribute name="ProjectSubType" value="新建项目"/>
12       <Attribute name="TemplateType" value="Project"/>
13     </Attributes>
14   </Content>
15 </VSContent>

2、将.vscontent与.zip文件压缩成.zip文件,后缀修改为.vsi,就是Visual Studio 安装程序 (.vsi) 文件。 

四、vsix安装程序

(vs2017, 自动安装模板,无需将wizard文件注册到gac中)

1、在扩展性选项下,新建vsix项目,添加.zip模板文件和wizard文件

2、打开source.extension.vsixmanifest,修改(参考https://msdn.microsoft.com/en-us/library/dd885241.aspx?f=255&MSPPError=-2147217396

1 Set the Product Name field to My Project Template.
2 Set the Product ID field to MyProjectTemplate - 1.
3 Set the Author field to Fabrikam.
4 Set the Description field to My project template.
5 In the Assets section, add a Microsoft.VisualStudio.ProjectTemplate type and set its path to the name of the .zip file.

  第5步:模板文件tw.zip, Type:Microsoft.VisualStudio.ProjectTemplate

              Source:File on System

      wizard文件,Type:Microsoft.VisualStudio.Assembly

              Source:File on System

              Embed in this folder: Assemblies

猜你喜欢

转载自www.cnblogs.com/xiaoyu1029/p/9251588.html