Prevent Visual Studio from adding default references and usings for new classes

丶灬走出姿态 提交于 2019-11-28 09:16:32

Marc and Brian both have a good idea: create a new custom template that includes only the usings and references I want. With Export Template it's really simple to do so, and I'll be sure to do so for all sorts of specific items.

For general-purpose new classes (ie: what you get from the "Add->Class..." menu item in VS), here's what I did to achieve my goal:

  • Find the appropriate template Zip. On my system it was located at C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
  • Extract the zip file. This gives two files: Class.cs and Class.vstemplate
  • Edit Class.cs to remove the undesired using statements. (I also changed the default class access modifier to "public" while I was here)
  • Edit Class.vstemplate to remove the undesired <reference> elements.
  • Rezip the files into the existing Class.zip archive
  • Replace the cached template files with the updated versions. On my system, the files were located at C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip (a directory containing the old Class.cs and Class.vstemplate).
    • I tried simply deleting this directory, expecting VS to rebuild the cache from the "original" source. This didn't work though; I got an error message saying that it couldn't find the files in the cache directory. Replacing the cached files worked well though.
  • Restart Visual Studio

Now, whenever I add a new class, I get exactly what I want.

You can change your template files... either by editing the files in the install location, or by writing a class how you want it, and choosing Export Template. There is also a template add-in somewhere...

You can create your own customized item templates, see

http://blogs.msdn.com/saraford/archive/2008/10/27/did-you-know-you-can-create-file-or-item-templates-343.aspx

for some details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!