Visual Studio tips: Create a code template with your own logo

Source: Internet
Author: User

After many blog posts, we all know how to use code segments. Using code segments can easily generate some common code formats, which is indeed very convenient for our development. During team development or in some cases, we often want to use the code generated by Visual Studio to automatically include some information, for example, the Code File Creation Time, code description, and reference to the namespace are supported by visual studio2005 and later versions.
The following uses the actual situation of Zhou Gong's notebook as an example to describe how to install the system on the C drive on Zhou Gong's Dell 1425 (the reason for mentioning this notebook is to remind everyone not to buy junk Dell, visual Studio 2005 and Visual Studio 2008 are installed on the system disk, and the genuine SQL Server 2005 is installed, because the system disk is often switched between Windows XP and Windows 7, and you want to share some personal configurations between Windows XP and Windows 7, therefore, set the directory of my documents to the last disk g in the system. The specific path is G:/my documents. If no special configuration is made, C: pay special attention to/Documents and Settings/zhoufoxcn/my documents.
Here we will describe Visual Studio 2008. First we open C: /program files/Microsoft Visual Studio 9.0/common7/ide directory (if Visual Studio 2005 is used, the corresponding path is C: /program files/Microsoft Visual Studio 8/common7/IDE), you can see the following situation:
 
We can see that the itemtemplates and projecttemplates folders are selected by Zhou Gong, because these folders are useful to us. The itemtemplates file contains the template we used to add files to the project, and the projecttemplates folder stores the template we created for the project as the name suggests. Templates in these folders will appear when we create files or projects.
In addition to these templates, Visual Studio also searches for the current user's personal template when creating the template. In this document, the path for storing the personal template for Visual Studio 2008 is G: /My Documents/Visual Studio 2008/templates (if this special setting is not passed, this path may be C: /login ents and settings/zhoufoxcn/My login ents/Visual Studio 2008/templates, where zhoufoxcn is the user name of the current logon system). Open this folder and we can see the following situation:
 
The two folders are empty when no configuration is made. If we add a file to the project, we will see this situation, for example:
 
As you can see, my template is empty.
All right, next week, we will copy the itemtemplates and projecttemplates folders under C:/program files/Microsoft Visual Studio 9.0/common7/ide to the current Personal Configuration folder G: /My Documents/Visual Studio 2008/templates (please note the relationship between your computer configuration and the path in my computer configuration), the system may prompt whether to overwrite, click "OK" and then create a file in any project. The following information is displayed, as shown in:
 
At this point, we can see that the new file types that can be created under my template are the same as those of the system. If we choose to create a file, the generated file content will be the same as that of the system template. To create code that belongs to our individual nature, we need to transform my template.
For example, if we want to change the default code generated by the system, we want to automatically add some information when creating the Code. For example, we want the default code to be public, follow these steps:
First, open the storage path of my template. Here, the path is G:/my templates/Visual Studio 2008/templates/itemtemplates (note that you may be different from Zhou Gong ), this folder contains VB.. net, C ++, and C #. If you want to change the C # file, open the C # file creation template, in this document, the path is G:/My Documents ents/Visual Studio 2008/templates/itemtemplates/CSHARP/code/templates, corresponding to adding classes, code files, interfaces, and.. NET Compact framework class template. Each zip package contains one. CS file and. vstemplate . In this week, We decompress class.zip. The code of the class. CS file is as follows:

Using system; <br/> using system. collections. generic; <br/> $ if $ ($ targetframeworkversion $ = 3.5) using system. LINQ; <br/> $ endif $ using system. text; </P> <p> namespace $ rootnamespace $ <br/>{< br/> class $ safeitemrootname $ <br/>{< br/>}< br/>} 
Change the Code as follows:

Using system; <br/> using system. collections. generic; <br/> $ if $ ($ targetframeworkversion $ = 3.5) using system. LINQ; <br/> $ endif $ using system. text; </P> <p> namespace $ rootnamespace $ <br/>{< br/> /// <summary> <br/> // prepared: zhoufoxcn <br/> // time: $ time $ <br/> // company: $ registeredorganization $ <br/> // copyright: $ year $-2012 <br/> // CLR version: $ clrversion $ <br/> // blog address: http://blog.csdn.net/zhoufoxcn <br/> // $ safeitemrootname $ Description: The copyright of this Code belongs to Zhou Gong, must be used with Zhou Gong blog address <br/> // unique identifier: $ guid1 $ <br/> /// </Summary> <br/> public class $ safeitemrootname $ <br/>{< br/> $ safeitemrootname $ () <br/>{< br/>}< br/>} 
Hosts file.
Now we can test our results. Open a project and follow the steps "add"-"new item" to add it, in the displayed dialog box, select "class" under "My template" (because we just changed the template of the class added to my template ), the code generated by Visual Studio 2008 according to the template is as follows:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace wcfclient <br/>{< br/> /// <summary> <br/> // prepared: zhoufoxcn <br/> // time: 9:39:04 <br/> // company: www.netskycn.com <br/> // copyright: 2010-2012 <br/> // CLR version: 2.0.50727.3603 <br/> // blog address: http://blog.csdn.net/zhoufoxcn <br/> // class1 description: this code is copyrighted by Zhou Gong and must contain the Zhou Gong blog address <br/> // unique identifier: f5d4bfbe-accd-4d77-92b0-1407db616ae1 <br/> /// </Summary> <br/> public class class1 <br/> {<br/> class1 () <br/>{< br/>}< br/>} 
From the above example, we can see that the items selected for my template will generate code according to the predefined format. If we have special format requirements for the code in the project, it is very convenient to customize it here, at least Zhou Gong will use this template to write the code published on his blog in the future, because Zhou Gong notices that some websites like to remove the link information and author information from others' blog posts, this actually violates the author's statement. If you have time, you can change your template for each item.
The above only demonstrates the configuration for generating a new file. In fact, we can also change the configuration of the new project. According to the previous operations, we also have the template options when adding a new project, as shown in:
 
However, even if you select to create a new project from my template, it is the same as the project in the system, because no special configuration has been made. We can modify the project templates under G:/My Documents ents/Visual Studio 2008/templates/projecttemplates/CSHARP. They are also a zip file. For example, if we want to unify the assembly information, you can modify assemblyinfo in this zip file. CS file, so you do not need to manually modify the assembly information every time.
After completing the above steps, we can easily customize templates for adding new projects or new projects, which is quite useful for team development in the company, it is also convenient to unify the format of code files.
When editing a template, you will see something like $ time $ and $ rootnamespace $. These are the variables used to generate the template, which variables can be used when editing a template? Microsoft does not have official materials. However, Zhou Gong noticed the following variables:
Itemname
Safeitemname
Sateitemrootname
Projectname
Safeprojectname
Rootnamespace
Guid [1-10]
Time
Year
Username
Userdomain
Machinename
Clrversion
Targetframeworkversion
Registeredorganization
Wizarddata
Some people may not understand the above variable names too well, but they are actually regular. They are the full names of a few words, but we didn't follow the naming rules of camel and Pascal (we can see that it is really hard to read a lot if we don't follow the naming Rules ), after knowing this rule, everyone can use these variables as their names name suggests. If they do not, they can be inferred based on the final generated code. In addition, if it is difficult to choose from "My template" every time, you can replace the template in the system with a custom template after modification. We also saw some simple logic when editing the template, such as the project to be used. when the Net Framework version is 3.5, system is automatically used. in the LINQ namespace, we can also add more logics in the model system template to better suit individual or company needs.
 
Zhou Gong
2010-06-20

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.