How to modify the default new class template for C# in Visual Studio 2008 or 2010?
可参考博客:
http://www.rhyous.com/2010/02/17/how-to-modify-the-default-new-class-template-in-visual-studio-2008/
http://www.thecodinghumanist.com/Content/HowToEditVSTemplates.aspx
1.StyleCop安装网址:http://stylecop.codeplex.com/可下载最新版本
实现前系统默认方式:
1 2 3 4 5 6 7 8 9 10 11 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyNameSpace { class MyClass { } } |
需要实现后的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyNameSpace { public class MyClass { #region Member Variables #endregion #region Constructors /// <summary> /// The default Constructor. /// </summary> public MyClass() { } #endregion #region Properties #endregion #region Functions #endregion #region Enums #endregion } } |
操作步骤:
1、找到Class.zip文件包。具体目录因操作系统不同而不同,如下:
Visual Studio 2008
· For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
· For 32 bit: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Visual Studio 2010
· For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
· For 32 bit: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
2、解压Calss.zip文件包找到并打开 Class.cs 文件.
文件内容如下::
1 2 3 4 5 6 7 8 9 10 11 | using System; using System.Collections.Generic; $if$ ($targetframeworkversion$ >= 3.5)using System.Linq; $endif$using System.Text; namespace $rootnamespace$ { class $safeitemrootname$ { } } |
3、将文件格式写成如下方式:
//-----------------------------------------------------------------------
// <copyright file="$safeitemrootname$.cs" company="Company">
// Copyright (C) Company. All Rights Reserved.
// </copyright>
// <author></author>
// <summary></summary>
//-----------------------------------------------------------------------
namespace $rootnamespace$
{
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
/// <summary>
///
/// </summary>
public class $safeitemrootname$
{
#region Fields
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the $safeitemrootname$ class.
/// </summary>
public $safeitemrootname$()
{
}
#endregion
#region Finalizers (Destructors)
#endregion
#region Delegates
#endregion
#region Events
#endregion
#region Enums
#endregion
#region Interfaces
#endregion
#region Properties
#endregion
#region Indexers
#endregion
#region Methods
#endregion
#region Structs
#endregion
#region Classes
#endregion
}
}
保存该文件并重新打包成Class.zip。(该文件已经默认写好全部集成在了Vss中的Class.zip内只需要覆盖)
Rebuild the zip file with the new Class.cs. Be careful to build the zip file correctly.
4、找到如下目录进行覆盖:
Visual Studio 2008
· For 64 bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
· For 32 bit: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Visual Studio 2010
· For 64 bit:
· C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
· C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\5052\Class.zip
·
· For 32 bit:
· C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
· C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\5052\Class.zip
·
5、以管理员身份打开“命令提示符”,执行如下命令
1. Visual Studio 2008
64-bit
cd C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\
32-bit
cd C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\
Visual Studio 2010
64-bit
cd C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\
32-bit
cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\
2. Run this command:
devenv.exe /installvstemplates
6、集成到VS2010
a.关闭工程,打开工程的.csproj文件准备编辑;
b.找到 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 这一行;
c.在该行下面加上 <Import Project="$(ProgramFiles)\MSBuild\StyleCop\v4.7\StyleCop.targets" />
d.保存文件。
7、出现CA0503,CA0505警告提示如何解决
集成到vs2010后会经常出现,CA0503,CA0505警告提示,该提示可以通过如下方式进行禁用。
A、通过注册表,运行regedit打开注册表找到下面节点:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Setup\EDev\CodeAnalysisErrorListViolationLimit
系统默认是最多200代码分析警告显示错误列表。查看更多警告,你必须修改或增加这些警告以提高性能。你通过改变注册表键中的这个值从而超过这个限度,,如把200改成1000,注意不能无限放大。
B、如果上述方法还是没能够解决请打开项目中的.csproj文件,查找下是否在删除<PropertyGroup>节点中含有CodeAnalysisRules/CodeAnalysisRuleSet 重新的生成下项目即可