ArticleDirectory
- 1. Problem Introduction
- 2. Environment Description
- 3. Knowledge preparation
- 4. Add templates
- 5 conclusion
- 6 References
1. Problem Introduction
When creating a project, you mustCodeSpecification and version information are indispensable, but it is troublesome and difficult to copy version information in the file header each time. In order to complete the project in a better and more standardized manner, the project file must be in a unified format.
2. Environment Description
OS: Windows 2003
Development Environment: vs2008
Language: C #
Architecture: c/s B/S
3. Knowledge preparation: 3.1 vs Project template and item template location
By default, templates installed with products are located in the following locations:
- /Common7/IDE/itemtemplates/Language/region settings/
- /Common7/IDE/projecttemplates/Language/region settings/
(Here the location is provided by Microsoft, which is different from the information in actual processing. The specific location is shown below)
3.2 vs template Introduction
Example of a template:
<Vstemplate type = "project" version = "2.0.0" Xmlns = "http://schemas.microsoft.com/developer/vstemplate/2005"> <Templatedata> <Name> My template </Name> <Description> A Basic starter kit </description> <Icon> templateicon. ICO </icon> <Projecttype> CSHARP </projecttype> </Templatedata> <Templatecontent> <Project file = "mystarterkit. csproj"> <Projectitem> form1.cs <projectitem> <Projectitem> form1.designer. CS </projectitem> <Projectitem> program. CS </projectitem> <Projectitem> properties/assemblyinfo. CS </projectitem> <Projectitem> properties/resources. resx </projectitem> <Projectitem> properties/resources. Designer. CS </projectitem> <Projectitem> properties/settings. Settings </projectitem> <Projectitem> properties/settings. Designer. CS </projectitem> </Project> </Templatecontent> </Vstemplate> |
3.2.1 vstemplate Element
The vstemplate element is the root element of the. vstemplate file. Its Attributes are as follows:
Attribute |
Description |
Type |
Identifies a template as a project template or item template. The value of this attribute can beProjectOrItem |
Version |
The template version number. Template in Visual Studio 2005VersionThe property value is2.0.0 |
Child Element
Element |
Description |
Templatedata |
Required element. Specify the data to be classified by this template, and define the display mode of this template in the "New Project" or "Add new project" dialog box. |
Templatecontent |
Required element. Specifies the template content. |
Wizardextension |
Optional elements |
Wizarddata |
Optional elements |
3.2.2 template parameters
All templates Support Parameter replacement during template instantiation to replace key parameters, such as class names and namespaces. When you click OK in the "New Project" or "Add new project" dialog box, these parameters will be replaced by the template wizard running in the background.
The template parameters are declared in the format of $ parameter $. For example, $ safeprojectname $.
The following table lists the template retention parameters:
Parameters |
Description |
Clrversion |
The current version of the Common Language Runtime Library (CLR. |
Guid [1-10] |
The GUID used to replace the project guid in the project file. You can specify up to 10 unique guid1 guids )). |
Itemname |
The name provided by the user in the Add new project dialog box. |
Machinename |
Current computer name (for example, computer01) |
Projectname |
The name provided by the user in the new project dialog box. |
Registeredorganization |
Registry Key Value in HKLM/software/Microsoft/Windows NT/CurrentVersion/registeredorganization |
Rootnamespace |
The root namespace of the current project. This parameter is used to replace the namespace in the items added to the forward project. |
Safeitemname |
The name provided by the user in the "Add new item" dialog box. The name is removed from all unsafe characters and spaces. |
Safeprojectname |
The name provided by the user in the "New Project" dialog box. All unsafe characters and spaces are removed from the name. |
Time |
Current Time in DD/MM/YYYY 00:00:00 format |
Userdomain |
Current User Domain |
Username |
Current User Name |
Webnamespace |
The name of the current website. Use this parameter in a web form template to ensure that the class name is unique. If the website is located in the root directory of the web server, the template parameters will be resolved to the root directory of the Web server. |
Year |
Current year in yyyy format |
4. Add templates
Some of the above knowledge points may be messy, but they will be used later, so we need to understand the above.
There are two types of adding templates: project templates when a new project is created and project templates when a new item is added to the project. The procedure is as follows:
● Create a template
● Copy the template to the specified location
● Resetting the vs Environment
4.1 Add a project template
Here, we will use a C/S winform template to describe how to add a template. Other project templates are similar. If the paths are different, the corresponding paths are provided.
4.1.1 create a template
Micro provides a template creation method:
● Copy the windowsapplication.zip file under/common7/IDE/projecttemplates/CSHARP/Windows/2052.
● Extract, 1
Figure 1 windowsapplication.zip
● Modify form1, first 2, and last 3. Note that the red position in the figure is the template parameter mentioned in 3.2.2.
Figure 2 before form1.cs Modification
Figure 3 after form1.cs is modified
● Modify the program. CS and assemblyinfo. CS files.
● Modify cswindowsapplication. vstemplate, 4
Figure 4 cswindowsapplication. vstemplate
● Modify windowsapplication. csproj because I have modified the file name of program. C. You do not need to modify this file unless you modify the file name.
● Compress all files into windowsapplication.zip. Note that folders cannot be added during compression.
Figure 5 compressed file
Now, the winform Project template is ready.
4.1.2 copy the template to the specified location
My approach is to copy all content in/common7/IDE/projecttemplates/CSHARP/windows to a new folder, such as comp windows, and delete all content in Comp Windows/2052, place the template generated above in the 2052 directory and copy the comp Windows file to/common7/IDE/projecttemplates/CSHARP/
4.1.3 resetting the vs Environment
Open the Visual Studio 2008 command line tool and execute devenv/setup. The path is as follows:
Figure 6: How to enable Visual Studio 2008
You can use this method to create your own template (winformProgram). Let's take a look at the effect: Open vs, create and create a project, as shown in:
Figure 7: final effect
4.1.4 Add a B/S Project template
The above is the method for adding a C/S Project template. The method for adding a B/S Project template is similar. The difference lies in the copy template, b/S directly copies the generated template to/common7/IDE/projecttemplates/CSHARP/web/2052. Remember to rename the compressed file to avoid overwriting the original file.
4.2 Add an item Template
The production process is the same as that of 4.1. Note that the position of the item template is the same.
C/s item template location:/common7/IDE/itemtemplates/CSHARP/code/2052 class template location;/common7/IDE/itemtemplates/CSHARP/Windows Forms/2052 form template location
Location of the B/S template:/common7/IDE/itemtemplates/web/CSHARP/2052, including class, ASP form, Web Service, and user control.
The copy method is the same as 4.1.4.
Final:
Figure 8: Add a new project
Figure 9: Newly Added class
5 conclusion
At this point, all the adding work is complete, and the vs template can customize parameters, but it is not used because it is not very useful here.
Note the following:
● Compression is zip.
● If the English version is used, change the 2502 directory to 1033. (This Article is not tested, I guess)
6 References
Name |
Source |
Create a project template |
Http://msdn.microsoft.com/zh-cn/library/xkh1wxd8.aspx |
Manually create a project template |
Http://msdn.microsoft.com/zh-cn/library/ms185291.aspx |
Replace parameters in the template |
Http://msdn.microsoft.com/zh-cn/library/ms185311.aspx |
Search for and organize project templates and item templates |
Http://msdn.microsoft.com/zh-cn/library/y3kkate1.aspx |
Vstemplate element (Visual Studio template) |
Http://msdn.microsoft.com/zh-cn/library/31cdwx28%28VS.80%29.aspx |
Template parameters |
Http://msdn.microsoft.com/zh-cn/library/eehb4faa.aspx |
Pass custom parameters to the template |
Http://msdn.microsoft.com/zh-cn/library/ms247063.aspx |
|
author: xzavier Source: http://blog.csdn.net/qiaozhiwei the copyright of this article is shared by the author and csdn. You are welcome to reprint it, but you must keep this statement without the consent of the author, the original Article connection is clearly provided on the Article Page, otherwise the right to pursue legal liability will be reserved. |