让VS自动添加注释
- 格式:doc
- 大小:108.50 KB
- 文档页数:17
VS 自动添加注释2011-07-16 12:41 171人阅读评论(1) 收藏举报From :/%C8%ED%BC%FE%CD%F8%C2%E7%B4%BF%BC%BC%CA%F5/blog/item/2baa 5512c12b6a916438db40.html现在大多数公司都规定程序员在程序文件的头部加上版权信息,这样每个人写的文件都可以区分开来,如果某个文件出现问题就可以快速的找到文件的创建人,用最短的时间来解决问题,常常是以下格式://======================================================================// All rights reserved//// description ://// created by User//======================================================================有些人使用Copy和Paste的方式,这样即浪费时间,效果又不是很好,就说上面的时间你就无法去Paste,哈哈,下面我就教大家怎样去添加这些信息,使我们的代码更有个性.1.在visual studio 2010的安装路径下如:[盘符]:\Program files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplatesCache2.找到文件夹如图所示:里面可以为各种语言进行修改.如果对WinForm中的类进行修改可以打开\CSharp\2052\Class.zip.其中\CSharp\2052\包括了所有WinForm文件类型的模板.打开Class.zip里面有一个Class.cs文件,我们对其进行修改,当我们在WinForm中添加类文件的时候,类文件就会自动添加上我们的信息.如下://======================================================================//// All rights reserved//// filename :$safeitemrootname$// description ://// created by User at $time$////======================================================================using System;using System.Collections.Generic;using System.Text;namespace $rootnamespace${class $safeitemrootname${}}$rootnamespace$为生成类的命名空间的名字,$safeitemrootname$为生成类的类名.可以看到我们在版权信息中加入了$time$,它就可以直接给我们加入创建的时间.我们可以对\CSharp\2052中所有的模板进行修改,切忌不要轻易修改系统那些代码,以免影响我们的正常的使用.对于做Web开发的人员来说可以在ItemTemplatesCache\Web\CSharp\2052里进行修改.新建一个类文件就可以实现了插入我们自定义的版权信息.//======================================================================//// All rights reserved//// filename :NewClass// description ://// created by User at////======================================================================using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;/// <summary>/// NewClass 的摘要说明/// </summary>public class NewClass{public NewClass(){//// TODO: 在此处添加构造函数逻辑//}}哈哈,是不是很酷呀!赶快个性一下你的代码吧!这个是扩展参数上面提供了部分的参数(希望有人继续补充),已经经本人在VS2010下测试,可以通过/*小补充- Start另外,类库Web或Winform都是不同的(Web的在WebClass.zip下面的Class.cs中,各种不同的类型,在的位置也不同)种类也各有不用,分别为Class.zip、Interface.zip、Form.zip等添加注释(我们是以Class.zip为例,也就是以类为基础)此外:这里$var$ 都是系统的变量,模板参数是要区分大小写的,大家需要注意。
系统提供的可用的参数如下:参数说明clrversion公共语言运行库(CLR) 的当前版本。
GUID [1-10]用于替换项目文件中的项目GUID 的GUID。
最多可以指定10 个唯一的GUID(例如,guid1))。
itemname用户在添加新项对话框中提供的名称。
machinename当前的计算机名称(例如,Computer01)。
projectname用户在新建项目对话框中提供的名称。
registeredorganizationHKLM/Software/Microsoft/Windows NT/CurrentVersion/RegisteredOrganization 中的注册表项值。
rootnamespace当前项目的根命名空间。
此参数用于替换正向项目中添加的项中的命名空间。
safeitemname用户在―添加新项‖对话框中提供的名称,名称中移除了所有不安全的字符和空格。
safeprojectname用户在―新建项目‖对话框中提供的名称,名称中移除了所有不安全的字符和空格。
time以DD/MM/YYYY 00:00:00 格式表示的当前时间。
userdomain当前的用户域。
username当前的用户名。
year以YYYY 格式表示的当前年份。
小补充- End */模板/****************************************************************************** filename :$safeitemrootname$* 描述:** 创建者User* CLR版本: $clrversion$* 新建项输入的名称: $itemname$* 机器名称: $machinename$* 注册组织名: $registeredorganization$* 命名空间名称: $rootnamespace$* 文件名: $safeitemname$* 创建系统时间: $time$* 创建年份: $year$*/*****************************************************************************/程序注释的重要性毋庸置疑,一个大型的项目通常情况下都是几个软件工程师合作出来的成果,假如该项目中的代码注释不全,那会给将来的维护者带来无穷无尽的隐患。
通用的办法是给自己工程里面的函数添加注释——使用宏。
1.打开Visual Studio 2008(2005一样适用)开发工具,单击―工具→宏→新建宏项目‖,然后按照步骤建立注释宏,添加如下代码并保存。
2.打开菜单–> 工具–>选项–> 键盘,在列表框中选择刚才添加的Macro,然后在按快捷键中输入快捷键,点击‖分配‖ 。
注释宏的代码如下:Sub AddFunComment()Dim DocSel As EnvDTE.TextSelectionDocSel = DTE.ActiveDocument.SelectionDocSel.NewLine()DocSel.Text = "/*******************************************************************"DocSel.NewLine()DocSel.Text = "* 函数名称:"DocSel.NewLine()DocSel.Text = "* 功能:"DocSel.NewLine()DocSel.Text = "* 参数:"DocSel.NewLine()DocSel.Text = "* 返回值:"DocSel.NewLine()DocSel.Text = "* 作者:Tanky Woo"DocSel.NewLine()DocSel.Text = "* 博客:(1) | (2)"DocSel.NewLine()DocSel.Text = "* 电子邮箱:admin@"DocSel.NewLine()DocSel.Text = "* 日期:" + System.DateTime.Now.ToLongDateString()DocSel.NewLine()DocSel.Text = "*******************************************************************/"End Sub宏代码示例2:Option Explicit OffOption Strict OffImports SystemImports EnvDTEImports EnvDTE80Imports EnvDTE90Imports EnvDTE90aImports EnvDTE100Imports System.DiagnosticsImports VSLangProjImports System.IOImports System.TextImports System.Collections.GenericImports System.Runtime.InteropServicesImports System.Windows.FormsPublic Module ModuleName'You can just define the variableDim document As Document = DTE.ActiveDocumentDim selection As TextSelection = DTE.ActiveDocument.SelectionDim headerText As StringDim filename As String = Dim pathname As String = document.Path.ToString()Dim projectname As String = Public Sub AddFileHeader()Try'Must set value again because there is singleton module instance document = DTE.ActiveDocumentselection = DTE.ActiveDocument.Selectionfilename = pathname = document.Path.ToString()projectname = selection.StartOfDocument()'deleteExistComment()insertComment()FinallyApplication.DoEvents()End TryEnd SubPrivate Sub deleteExistComment()selection.StartOfDocument()DTE.ExecuteCommand("Edit.Find")DTE.Windows.Item(filename).Activate()DTE.Find.FindWhat = "using system"DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentDTE.Find.MatchCase = FalseDTE.Find.MatchWholeWord = FalseDTE.Find.Backwards = FalseDTE.Find.MatchInHiddenText = TrueDTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteralDTE.Find.Action = vsFindAction.vsFindActionFindIf (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) ThenReturnEnd IfDTE.Windows.Item(filename).Activate()DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColu mn)DTE.ActiveDocument.Selection.LineUp(True, 100)DTE.ActiveDocument.Selection.Delete()DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close() End SubPrivate Sub insertComment()'selection.StartOfDocument()selection.Insert("#region (C) Header Region @ " + Date.Today.Year.ToString())selection.NewLine()selection.Insert("//============================================================= =")selection.NewLine()selection.Insert("//")selection.NewLine()selection.Insert("// The Herder Region@ " + Date.Today.Year.ToString())selection.NewLine()selection.Insert("// Copyright (C) 2010 - " + Date.Today.Year.ToString() + ". All rights reserved.") selection.NewLine()selection.Insert("//")selection.NewLine()selection.Insert("//")selection.NewLine()selection.Insert("// File:")selection.NewLine()selection.Insert("// " + filename)selection.NewLine()selection.Insert("//")selection.NewLine()selection.Insert("// Description: Why do you create this file ")selection.NewLine()selection.Insert("//")selection.NewLine()selection.Insert("//============================================================= =")selection.NewLine()selection.Insert("//")selection.NewLine()selection.Insert("// $History: " + document.FullName.Substring(document.FullName.IndexOf(":") + 1) + " $")selection.NewLine()selection.Insert("//")selection.NewLine()selection.Insert("// ****************** Version 1 ******************")selection.NewLine()selection.Insert("// User: Who Time : " + Date.Now.ToLocalTime().ToString())selection.NewLine()selection.Insert("// Updated in: " + projectname + " Project ")selection.NewLine()selection.Insert("// Comments: What do you want to do ")selection.NewLine()selection.Insert("// ")selection.NewLine()selection.NewLine()selection.Insert("#endregion")selection.NewLine()selection.NewLine()End SubEnd Module================================================================以下的宏定义可以拿来作为学习宏操作之用From : /blog/static/27710872200972815439359/创建过程:1. 新建Macro工程打开菜单-->工具--> 宏--> 新建宏项目...,根据向导提示建立工程。