【问题标题】:Visual Studio 2013 - Edit Project TemplateVisual Studio 2013 - 编辑项目模板
【发布时间】:2015-01-12 21:41:27
【问题描述】:

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

【问题讨论】:

  • 你用的是什么版本的VS?社区(免费)版本不支持/installvstemplates 选项。

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


【解决方案1】:

Community/Express(免费)版本不支持/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(否则更改将在您下次运行时才会显示)
  • 打开命令提示符(如果您不是机器管理员,则应以管理员身份运行)。
  • 更改为模板文件夹上方几级的 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

【讨论】:

  • 我正在使用 VS 2013 Express。顺便说一句,我在“C:\\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\”中没有“C/C++”文件夹。那么,有了 VS2013E 我什么都做不了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-16
相关资源
最近更新 更多