您的位置:首页 > 编程语言 > C#

C#,C++修改vs文件模板,添加自定义代码版权版本信息

2016-09-01 11:05 741 查看
简单型的修改类似该路径下的模板文件即可(vs版本或安装路径不同路径可能不同)

C#:

模板参数参考https://msdn.microsoft.com/zh-cn/library/eehb4faa.aspx

<path>\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates[Cache]\CSharp\Code\2052\Class\Class.cs


例如添加如下内容

/**************************************************************
*  Filename:    $safeitemrootname$.cs
*  Copyright:   $registeredorganization$ Co., Ltd.
*
*  Description: $safeitemrootname$ ClassFile.
*
*  @author:     wjshan0808
*  @version     $time$  @Reviser  Initial Version
**************************************************************/


$time$参数的值太长,不合适,如果有$date$最好,可惜默认的没有,那就来添加一个.

步骤:

1.创建强命名程序集

using System;
using System.Collections.Generic;
//添加程序集引用
//  Microsoft.VisualStudio.TemplateWizardInterface.dll
//  EnvDTE.dll
//
using Microsoft.VisualStudio.TemplateWizard;
using EnvDTE;

namespace CustomParameter
{
//IWizard 接口说明
//https://msdn.microsoft.com/zh-cn/library/microsoft.visualstudio.templatewizard.iwizard(v=vs.80).aspx

public class CustomParameters : IWizard
{
public void RunFinished()
{
//throw new NotImplementedException();
}

public void RunStarted(object automationObject, System.Collections.Generic.Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
try
{
//添加自定义模板参数 Date
string key = "$Date$";
if (replacementsDictionary.ContainsKey(key))
replacementsDictionary.Remove(key);
replacementsDictionary.Add(key, System.DateTime.Now.ToString("MM/dd/yyyy"));
//可以扩展一个修订者
//string extendKey = "Reviser";
//if (replacementsDictionary.ContainsKey(extendKey))
//    replacementsDictionary.Remove(extendKey);
//replacementsDictionary.Add(extendKey, "[弹窗/读文件/...]获取信息");
}
catch (Exception ex)
{

}
}
// This method is only called for item templates,
// not for project templates.
public bool ShouldAddProjectItem(string filePath)
{
return true;
}

public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
{
//throw new NotImplementedException();
}

public void ProjectFinishedGenerating(EnvDTE.Project project)
{
//throw new NotImplementedException();
}

public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
{
//throw new NotImplementedException();
}
}
}


可以用sn工具生成.snk文件,也可以左键项目属性->签名创建.snk文件。

项目文件AssemblyInfo.cs中添加snk文件

[assembly: AssemblyKeyFile("CustomParameter.snk")]


/keyfile: https://msdn.microsoft.com/zh-cn/library/wb84w704.aspx

此操作会出现 <...使用命令行选项“/keyfile”或适当的项目设置代替“AssemblyKeyFile”...>警告,消除警告重新回到属性签名选择生成的snk即可。

使用开发人员命令提示工具

提取公钥到CustomParameter.psnk

>sn -p CustomParameter.snk CustomParameter.psnk


查看公钥信息

>sn -tp CustomParameter.psnk


2.部署到GAC(global assembly cache)

使用管理员身份运行开发人员命令提示工具然后使用GACUtil工具部署程序集

>GACUtil /i CustomParameter.dll


成功标记:程序集已成功添加到缓存中。

程序集卸载(如果代码更新出错什么的..!)

>gacutil /u CustomParameter,Version=1.0.0.0,Culture=Neutral,PublicKeyToken=1d6512175a2e39a6


重新安装

>GACUtil /i CustomParameter.dll /f


3.查看程序集信息

>gacutil /l CustomParameter


全局程序集缓存包含下列程序集:
CustomParameter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1d6512175a2e39a6, processorArchitecture=MSIL

项目数 = 1


4.修改模板文件

<path>\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates[Cache]\CSharp\Code\2052\Class\Class.vstemplate


参考:https://msdn.microsoft.com/zh-cn/library/ms185301.aspx

添加程序集块

<WizardExtension>
<Assembly>CustomParameter, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=1d6512175a2e39a6</Assembly>
<FullClassName>CustomParameter.CustomParameters</FullClassName>
</WizardExtension>


到此看看效果

/**************************************************************
*  Filename:    Class2.cs
*  Copyright:   Microsoft Co., Ltd.
*
*  Description: Class2 ClassFile.
*
*  @author:     wjshan0808
*  @version     09/01/2016  @Reviser  Initial Version
**************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Class2
{
}
}


View Code
@Reviser修订者值可以加一个自定义参数值

</TemplateContent>
。。。
<CustomParameters>
<CustomParameter Name="@Reviser" Value="wjshan0808"/>
</CustomParameters>
</TemplateContent>


$registeredorganization$值是微软的。

读的是HKLM\Software\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization项值,我想改,不敢改,怕出错。

更可取的方式是自定义一个。

C++:

vs低版本可以用宏,我的2010看网上的教程用宏结果没效果,因为不懂VB,也不懂宏,不知道怎么解决了。先把代码记下。

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module Comments
Public Sub FileSign()
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection

'活动点移到文件开头
DTE.ActiveDocument.Selection.StartOfDocument()

DocSel.Text = "/**************************************************************"
DocSel.NewLine()
DocSel.Text = " *  Filename:" + "\t" + DTE.ActiveDocument.Name
DocSel.NewLine()
DocSel.Text = " *  Copyright:" + "\t" + "XX Co., Ltd."
DocSel.NewLine()
DocSel.Text = " *  Description:" + "\t" + ""
DocSel.NewLine()
DocSel.Text = " *  @author:" + "\t" + "wjshan0808"
DocSel.NewLine()
DocSel.Text = " *  @version" + "\t" + "" + Date.Today.ToString("MM/dd/yyyy") + "   wjshan0808   Initial Version"
DocSel.NewLine()
DocSel.Text = " *@"
DocSel.Text = " **************************************************************/"
DocSel.NewLine()
DocSel.NewLine()
End Sub

Public Sub FuncSign()
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.NewLine()
DocSel.Text = "/**************************************************************"
DocSel.NewLine()
DocSel.Text = " *  Description."
DocSel.NewLine()
DocSel.Text = " *  "
DocSel.NewLine()
DocSel.Text = " *  @param  -[in,out]  Type  name: [commet]"
DocSel.NewLine()
DocSel.Text = " *  @return  Value"
DocSel.NewLine()
DocSel.Text = " *  "
DocSel.NewLine()
DocSel.Text = " *  @version" + "\t" + Date.Today.ToString("MM/dd/yyyy") + "  wjshan0808  Initial Version"
DocSel.NewLine()
DocSel.Text = " **************************************************************/"
End Sub

End Module


最终用了个折中的方式,修改了新建C++.h.cpp文件,添加了个非智能的版本信息注释,再用工具做方法的注释

<path>\Microsoft Visual Studio 10.0\VC\vcprojectitems\hfile.h
<path>\Microsoft Visual Studio 10.0\VC\vcprojectitems\newc++file.cpp


这样的

/**************************************************************
*  Filename:    @OutputName.cpp
*  Copyright:   @Company Co., Ltd.
*
*  Description: 模板测试源文件.
*
*  @author:     @Author
*  @version     @MM/@dd/@yyyy  @Reviser  Initial Version
**************************************************************/

#pragma once

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

className::className()
{

}

className::~className()
{

}

/**
*  测试方法.
*
*  @param    -[in,out]  char*  pName: [测试参数]
*
*  @return   0.
*
*  @version  09/01/2016    @reviser  Initial Version
*/
long className::Test(char* pName)
{

}


/**************************************************************
*  Filename:    @OutputName.h
*  Copyright:   @Company Co., Ltd.
*
*  Description: 模板测试头文件.
*
*  @author:     @Author
*  @version     @MM/@dd/@yyyy  @Reviser  Initial Version
**************************************************************/

#pragma once

#include <stdio.h>

class className //: public baseClass
{
//private:
int m_Test;

public:
className();
virtual ~className();

public:
long Test(char* pName);

protected:
bool m_Ok;

};


用第三方工具推荐一个收费的http://www.atomineerutils.com/download.php,试了一下挺好用。可惜收费。

项目文档模板代码 http://pan.baidu.com/s/1eSC1a0a
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: