Using Visual Studio to write code requires creating a project (well, you can use Visual Studio without a project, but you won’t get very much past Notepad functionality – there’s not even Intellisense on files) . After creating / opening an existing project, we can add new files (classes, class diagrams, XML files etc) by selecting the Project menu, then Add New Item (or add Class / Windows Forms / Windows Component or User Control, if this is what you need). After you specify all the details (file name), a file is added with some default text in it. Assuming you have Visual Studio 2008 SP1 installed, the templates that generate this text are stored at %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates and %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplatesCache (for x64 machines, look in % ProgramFiles(x86)%).
Let’s add a new C# class, Tester, to the class library project SimpleTests. By default, in Visual Studio 2008 it will contain the following text:
This text comes from the Class template. If we look in the ProjectTemplateCache folder, inside CSharp (the language we used to add the class) -> Code -> 1033 -> Class.zip, we find a class.cs looking like this:
This explains the text we get. But how can we customize the default? I really would appreciate some changes:
– I don’t want all these usings added by default – I prefer to start without any using statement, and to add them while the code requires them. Of course, I could do it the other way around: add all the usings I think I may need; when I am done coding, remove the unused ones. Visual Studio lets you do that easily by right clicking on the page, selecting Organize Usings -> Remove Unused Usings (or Remove and Sort).
– Second, I prefer Usings inside the namespace declaration and I want to add a header to the file.
I can change the Class.cs file and replace the Class.zip in ProjectTemplatesCache folder:
Then when I reopen Visual Studio and Add a class, I get this file:
This change will be lost the first time when devenv /installvstemplates command is called (this command replaces the ItemTemplatesCache folder with what finds in ItemTemplates folder). To make it permanent, make the change in ItemTemplates folder instead and run devenv /installvstemplates to propagate the change to ItemTemplatesCache.