Visual Studio 技巧1 - 创建类时自动添加版权说明和作者信息

     写代码时,我们一般需要在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$
    {
    }
}
这里面可以用一些参数,下面列出一些:itemname, safeitemname, safeitemrootname, projectname, safeprojectname, rootnamespace, guid[1-10], time, year, username, userdomain, machinename, clrversion, registered-organization, wizarddata 。具体代表的含义大家都可以猜得出来。

     三、部署模板。部署模板也很简单,只需要覆盖原来的模板,然后重新安装一下模板即可。可以用一段脚本来执行。
SET ROOT=C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\
XCOPY Class.zip "%ROOT%ItemTemplates\CSharp\Code\1033\" //y
XCOPY Interface.zip "%ROOT%ItemTemplates\CSharp\Code\1033\" //y
"%ROOT%devenv.exe" /InstallVSTemplates
PAUSE
其中devenv.exe /InstallVSTemplates语句注册位于\Common7\IDE\ProjectTemplates 或\Common7\IDE\ItemTemplates 中的项目或项模板,这样就可以通过“新建项目”和“添加新项”对话框访问它们。

     这只是一个小小的技巧,但是我觉得非常管用。特别是在开发团队中,如果给每一个开发人员部署一个这样的模板,那么他们就不会常常忘记添加版权信息。这个方法其实很有用,你也可以部署其他常用的代码模板。


posted @ 2008-06-04 21:53  周锋(Andrew)  阅读(1469)  评论(0编辑  收藏  举报