写代码时,我们一般需要在Code File的最前面声明版权信息,作者的联系方式等。一些高级的IDE工具提供修改代码模板的功能,这样就能够在创建新代码时自动加上版权信息等。Visual Studio虽然没有提供这样的操作界面,但是我们可以通过手动修改原始代码模板来达到这个目的。
一、首先找到模板目录,例如我安装的Visual Studio 2008在C盘,则对应的模板目录是C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\ 。这个目录包含了所有模板,都是以zip文件存在的。这里举例C#的Class.zip和Interface.zip模板,可以在C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\下找到那两个zip文件。注意路径中的1033表示是英文版,如果是中文版,应该是2052。
二、修改模板。修改模板其实很简单,就是把那两个zip文件解压缩,修改里面的cs文件,然后重新压缩成一样的zip包。比如我修改的Class模板文件如下:
// Copyright (C) $year$ Jeteam Inc. All rights reserved.
//
// The information contained herein is confidential, proprietary
// to Jeteam Inc. Use of this information by anyone other than
// authorized employees of Jeteam Inc is granted only under a
// written non-disclosure agreement, expressly prescribing the
// scope and manner of such use.
// ==============================================================
// Create by Andrew Zhou at $time$.
// Version 1.0
// Andrew Zhou [mailto:fzhou.andrew@gmail.com]
// ==============================================================
using System;
using System.Collections.Generic;
$ if $ ($targetframeworkversion$ == 3.5 ) using System.Linq;
$endif$ using System.Text;
namespace $rootnamespace$
{
/// <summary>
///
/// </summary>
class $safeitemrootname$
{
}
}
三、部署模板。部署模板也很简单,只需要覆盖原来的模板就可以。把自己重新制作的zip包放入到\ItemTemplates\CSharp\Code\1033\下面去覆盖原来的同名zip文件就可以了。
注意:可能完成上边操作后,重新建立一个class的时候,发现VS并没有使用我们定制的模板,这个原因就是一个缓存问题。VS会在C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\文件夹中去寻找同名的模板文件,所以我们要也把这里改了,注意,这里的.zip结尾的只是一个文件夹,并不是一个压缩文件。
参考内容 http://www.cnblogs.com/andrewzhou/archive/2008/06/04/visual_studio_comment_copyright.html