Visual Studio 2013 - 编辑项目模板

时间:2014-11-15 16:22:49

标签: visual-studio templates visual-studio-2013

我通过“文件 - >导出模板”命令创建了一个新模板。我想要源文件名如项目名称,所以,我编辑了“.vstemplate”文件。但是现在 - 我如何使用这个更新的模板?似乎VS在其他地方保留模板的副本,因为在重新启动VS后 - 没有任何改变。 顺便说一句,在VS2012 by developer命令提示符下可以通过命令“devenv / installvstemplates”安装修改过的模板。现在,这个命令消失了,它不是命令或文件。 那么,我该如何使用这个更新的模板文件?

1 个答案:

答案 0 :(得分:2)

社区/快递(免费)版本不支持/installvstemplates选项。

如果您使用的是其他版本,则自定义现有模板的步骤如下(摘自我的博客:http://blog.hitechmagic.com/?p=527):

<强> 1。查找文件

文件没有移动太多,因此目前位于C:\<Program Files>\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

(在VS2012中,他们在C:\<Program Files>\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

您想要的文件位于适当命名的文件夹中(不再有ZIP文件!)。如果您打开Class文件夹,您将找到以下2个文件:

Class.cs
Class.vstemplate

<强> 2。备份原始文件!

非常重要的一步,因为事情出错了。备份原始文件,例如通过将其复制到Class.cs。原始,以便Visual Studio无法识别。

第3。更改Class.cs模板文件

这是您发挥创意的地方。您会注意到原始内容如下所示:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

你可能想要更像这样的东西:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
        #region Constants
        #endregion Constants

        #region Fields
        #endregion Fields

        #region Properties
        #endregion Properties

        #region Constructors
        #endregion Constructors

        #region Public methods
        #endregion Public methods

        #region Class override methods
        #endregion Class override methods

        #region Class extensions - virtual methods
        #endregion Class extensions - virtual methods

        #region Private helper methods
        #endregion Private helper methods
    }
}

您会注意到模板中有许多宏命令。完整列表位于:http://msdn.microsoft.com/en-us/library/eehb4faa.aspx

<强> 4。保存更改 只需保存已编辑的文件(再次,不再使用VS 2012/2013重新压缩ZIP文件)。

<强> 5。告诉Visual Studio有关更改 除非您明确告诉Visual Studio重新加载所有模板,否则不会加载您的新更改。

  • 关闭Visual Studio(或更改swill在下次运行之前不显示)
  • 打开命令提示符(如果您不是该计算机的管理员,则应以管理员身份运行此命令)。
  • 更改模板文件夹上方几级的IDE文件夹(例如,更改为C:\<Program Files>\Microsoft Visual Studio 10.0\Common7\IDE
  • 运行以下命令:devenv.exe /installvstemplates
  • 中提琴!你有自己的新课程自定义模板。

<强> 6。自动执行重复/繁琐的任务

如果您打算这么做,我建议您创建一个批处理文件,例如:在您的模板文件夹中,为您运行更新步骤。例如,创建一个名为UpdateTemplates.bat的文本文件,其中包含以下3行:

cd "C:\Prograsm Files (x86)\Microsoft Visual Studio 12.0\Common\IDE\"
devenv.exe /installvstemplates
pause