Visual Studio custom template (1)

Source: Internet
Author: User

Each company has its own coding standard. The most basic one is that a file must have a file header, but the default template in Visual Studio does not have any file header information, therefore, we need to define our own template.

Templates in Visual Studio are divided into two types: project templates and item templates. A project template is an application template when we add a project, such as a Windows application.Program, Class library, etc., appear in the "Add new project" dialog box, such,

 

When we add a new item in a project, such as adding a class, an interface, and so on, this is an application template, which appears in the "Add new item" dialog box, for example,

This articleArticleThis section describes how to customize a custom template.

Defining Your template in Visual Studio has a very quick way. First, create a new file and define the template format we want, then, use the [Export TEMPLATE] sub-menu under the [file] menu to generate a template, for example,

The following is a simple template I have defined,

 # Region  Copyright (c) rainsoft All Rights Reserved  
/* **************************************** **************************************** **********
* Creation:
* Author: $ username $
* Date: $ time $
* Description:
* Version:
* Modification:
* Author:
* Date:
* Description:
* Version:
**************************************** **************************************** ********** */
# Endregion

Namespace $ Rootnamespace $
{
Using System;

Public Class $ Itemname $
{
}
}

 

 
 

You can use [Export TEMPLATE]-> select an item template to quickly generate a custom template. After the template is generated, the template we just defined appears in the "Add new project" dialog box, as shown in,

The following is a class generated using this template,

  # Region  Copyright (c) rainsoft All Rights Reserved 
/* **************************************** **************************************** **********
* Creation:
* Author: arrui
* Date: 19:33:51
* Description:
* Version:
* Modification:
* Author:
* Date:
* Description:
* Version:
**************************************** **************************************** ********** */
# Endregion

Namespace Mytemplate
{
Using System;

Public Class Myclasstemplate2
{
}
}

 

 
 
 
 

Some template parameters such as $ rootnamespace $ and $ username $ are used in the template file we just defined. These are some default template parameters provided by Visual Studio. These parameters include

parameter description
clrversion current version of the Common Language Runtime (CLR ).
guid [1-10] a guid used to replace the project guid in a project file. you can specify up to 10 unique guids (for example, guid1 ).
itemname the name provided by the user in the Add new item dialog box.
machinename the current computer name (for example, computer01 ).
projectname the name provided by the user in the new project dialog box.
registeredorganization the registry key value from HKLM \ Software \ Microsoft \ Windows NT \ CurrentVersion \ registeredorganization.
rootnamespace the root namespace of the current project. this parameter is used to replace the namespace in an item being added to a project.
safeitemname the name provided by the user in the Add new item dialog box, with all unsafe characters and spaces removed.
safeprojectname the name provided by the user in the new project dialog box, with all unsafe characters and spaces removed.
time the current time in the format DD/MM/YYYY 00:00:00.
userdomain the current user domain.
username the current user name.
webnamespace the name of the current web site. this parameter is used in the web form template to guarantee unique class names. if the web site is at the root directory of the web server, this template parameter resolves to the root directory of the Web server.
Year The current year in the format YYYY.

(From msdn: MS-help: // Ms. VSCC. v90/ms. msdnqtr. v90.en/dv_vssoln/html/1b567143-08c6-4d7a-b484-49f0671754fe.htm)
Note that these template parameters are sensitive to size.
In general, using these template parameters is enough for us to define some of the templates we use on a daily basis, but for some perfectionists (such as me, :-), these are not enough, for example, the Date Format I want to generate does not contain the time, and the month/day is a fixed two-digit, which is used in the middle. for example, 2008.10.06. Because the template parameters provided by Visual Studio by default cannot allow us to customize the format, we need to customize the template parameters to implement the above functions, this is also the topic of my next article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.