I'm always looking for ways to save a bit of time or monotony when it comes to writing code in the office. The majority of our projects are typical corporate apps (read: boring as h*ll), so any little trick that can save me a few minutes and get me on to more interesting tasks quicker is welcome. With that in mind, I began looking at code templates, customizing project items, etc, etc... some time ago. Initially, I was looking for a way to add a custom project item which would add a strongly-typed collection to the current project. I was working on a project that had quite a few collection classes, and re-writing the same code over and over was groing tiring. Sure, it's not complex code, but there had to be a better way. I'm not much for code generators - I'm extremely picky about the end-result of such systems - and at the time (over a year ago) I wasn't aware of any add-ins that could help me in my quest. With a little help from an article by Chris Sells, I was able to get my custom item added to the “Add New Item” dialogue in VS.NET, complete with an HTML-based GUI allowing me to specify the contained class name, the collection class name, whether I wanted the collection to be read-only and a couple of other options. A few key-strokes and a couple of check-box selections later and my strongly-typed collection was added to my project. All in all, I was pleased. Heck, it even had our corporate logo at the top of the GUI.
This evening, I've spent a few hours exploring another type of customization: custom projects. I wanted the ability to click “New Project”, select “Windows Form (AVI)” from the list and have a project generated with a few extra files, a few extra default references and a few settings different than the standard “Windows Form” project. I considered using my previously gained knowledge of the VS.NET projects/items system to just alter the standard “Windows Form” project, but I don't really like doing such things. I did it with the ST Collection wizard I described above and had to “re-do” it when VS.NET 2003 came out (and even lost the customization once when I hosed VS.NET 2002 and had to re-install, forgetting to save my customizations first). So, I started digging. Turns out, it's not that hard.
Adding a sub-key to HKLM\SOFTWARE\Microsoft\VisualStudio\7.1\NewProjectTemplates\TemplateDirs\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC} got me a new “Project Types” entry shown when “New Project” is selected in VS.NET. I pointed the key's “TemplatesDir” value to a folder of my choosing where I could store my project wizards so I don't have to worry about losing them. I copied the existing “CSharpEXE.vsz“ file and the “CSharpEXEWiz“ folder into my custom folder and then tweaked the vsz file to point to the location of my “CSharpEXEWiz“ folder. This one required the path to be relative to VS.NET's original path, but the error displayed when it didn't work initially was sufficient to figure out what was going on. Getting custom project items listed was pretty much the same process. First, a new sub-key was added to HKLM\SOFTWARE\Microsoft\VisualStudio\7.1\Projects\{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\AddItemTemplates\TemplateDirs\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}, with it's name matching the previously added sub-key (”/2” for both in my case). Again, I set the “TemplatesDir” value to another folder of my choosing. Next, I copied the contents of the existing “CSharpProjectItems” folder within the VS.NET folder hierarchy into my custom folder, along with the “CSharpAddClassWiz“ folder. Again, I tweaked a vsz file - this time the “CSharpAddClassWiz.vsz” file - to point to my customized “CSharpAddClassWiz” folder. That's about it!
So now, we can go into VS.NET and, after clicking “New Project”, we will see an “AVI Projects” folder with a “Windows Forms” option (only one for now - probably do a custom “Class Library” option tomorrow). Starting from that project instead of the default gets us a customized app.config file, a customized AssemblyInfo.cs file, additional “using“ statements and additional references already added. After that, when clicking “Project | Add New Item...” we're greeted by an “AVI Project Items” folder with our customized options available (such as “Class”, which has our standard “using” statements already added, our standard “regions” defined, etc, etc...). I've left out a few of the details on how this is all done, but anyone who's interested is welcome to ask for help. And anyone who's done some customizations like this, let me know about your own experiences.