There are a multitude of reasons for altering a template. For instance you probably do not write new applications much but instead work on one project for a long time then move to a new project. Usually developers writing a new Windows forms application have several mundane tasks to perform, ranging from adding staple code modules, global exception handlers, menus etc. By selecting the default Windows forms application template none of the above is there, so we must manually complete these tasks.
Sure you can simply create a project and export it as a template also which is another option.
A better solution is to have the default windows form template to have staple modules, global exception handlers, menus etc. done when selecting a new project.
Modifications to a template (see attached project for a good example)
- Find code modules you want to include into the modified template.
- Close VS2008.
- Open the folder C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplatesCache\VisualBasic\Windows\1033
- Create a complete backup of the folder WindowsApplication.zip which can be done by renaming it to say BACKUP_WindowsApplication.zip.
- Create a new folder called WindowsApplication.zip
- Copy contents from BACKUP_WindowsApplication.zip into WindowsApplication.zip
- Copy files from step one into WindowsApplication.zip
- Open windowsapplication.vbproj with notepad. Find the line <COMPILE Include="Form1.vb">
- Directly above you will see <ITEMGROUP>
- Between these two lines insert the modules using the example below. So you have a module named ConvertMethods.vb we add the following which includes a folder destination for the module. The folder will be created when using this template and the module will be placed into the folder.
- The folder name is your choice. Repeat this step for all modules, classes and other files you are including. <COMPILE Include="Extensions\ConvertMethods.vb" />
- To add References simply locate the item group directly above step 9 and 10, grab the reference from another project by editing the project file in VS2008 and use it here.
- Save and close windowsapplication.vbproj.
- Now locate windowsapplication.vstemplate and open it in Notepad.
- Find the line containing TargetFileName="My Project\AssemblyInfo.vb", select the entire line and copy it above.
- In the copied line (using ConvertMethods.vb as in step 11) change TargetFileName="My Project\AssemblyInfo.vb" to TargetFileName="Extensions\ConvertMethods.vb" .
- Repeat step 16 for each added module, followed by saving the file.
- Fire up VS2008 and create a new Windows forms application. If you followed the steps above your additions will be in the project. If not, research what went wrong and correct it or simply close VS2008 and delete the folder WindowsApplication.zip and rename
- BACKUP_WindowsApplication.zip to WindowsApplication.zip. Restart VS2008 and you are back to the original template.
See attached WinZip file for a basic template and also a demo project showing additions to the default template in action. I kept the demo project simple for ease of learning. I mentioned in related articles "Extension methods" simply because the basic template and demo project have extension method within that you can use in your daily coding outside of the content here.