I <3 Steve McConnell*
Coding Horror
programming and human factors
by Jeff Atwood

February 21, 2006

C# Snippet Parity

Microsoft recently released a complete set of C# code snippets for Visual Studio 2005. This brings C# to parity with VB.NET, which had many more code snippets "in the box".

Unfortunately, Microsoft's installation strategy for these new snippets leaves a lot to be desired. You can download and "install" all the snippets at once, but you must manually add each of the snippet folders via the Snippet Manager (Ctrl+K, Ctrl+B). I loves me some snippets, but this is unacceptable!

The snippets themselves, of course, are just individual XML files that end in .snippet sitting in a bunch of folders. There are two valid snippet paths: per-user and system-wide. The system-wide C# snippet path is:

C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\

There's an index file in that folder, SnippetsIndex.xml, which appears to store the snippet paths. You might think editing this file would cause the folders to appear in the snippet browser (Ctrl+K, Ctrl+X). Seems logical, right?

Unfortunately, it's not that easy.

I think that file may be used the first time the IDE is initialized, but edits to that file don't appear in the Snippet Manager.

The real snippet paths are stored in the registry at:

[HKCU\Software\Microsoft\VisualStudio\8.0\Languages\CodeExpansions\Visual C#]

Why they chose to store this stuff in the registry and in an XML file is beyond me. Anyway, I packaged the correctly pathed snippets and a registry file together. Now you can quickly get the snippets installed on your system without running a pointless installer that does virtually nothing for you.

You can go from tired ..

Aww, C# has hardly any snippets

.. to wired ..

Just like VB.NET snippets! Viva le squiggly!

.. in no time at all. If you've forgotten the keyboard shortcuts for snippets, revisit my snippet blog entry to refresh your memory.

Microsoft provides 295 new C# code snippets, and they're 508 KB in total size. I chose to package the snippets as a 7zip file for compression efficiency:

  • ZIP: 229kb
  • RAR: 73kb
  • 7Zip: 56kb

To be fair to ZIP, it probably would have done better if the format supported solid archiving for this set of small files.

To decompress the archive, I recommend switching to WinRAR if you aren't already. It supports 7zip as well as RAR and ZIP, but more importantly, it won the best archive tool DonationCoder roundup. I'm not a huge fan of the UI, but I encounter RAR files often enough that WinZIP isn't cutting it any more.

Download C# snippets and registry file (60kb 7zip archive)

Posted by Jeff Atwood    View blog reactions

 

« Will Mouse Gestures Ever Be Mainstream? Sometimes a Word is Worth a Thousand Icons »

 

Comments

First post!

No, seriously, thanks for doing this. This just about makes up for the loss of an evening on Darwinia, which is totally your fault.

As for 7z, I totally agree that it's a better format, but since it's still a 733t h4x0r format I usually make them self extracting exe's (sfx's). Dunno if WinRAR supports it, but the 7zip program does.

Jon Galloway on February 21, 2006 08:28 PM

Thanks! Much improved over MS' "installation"

matt on February 21, 2006 08:34 PM

Leave it to a VB developer to make it easier... ;-)

VBMan on February 22, 2006 12:24 AM

Jeff man, thanks for this! We owe ya.

Scott on February 22, 2006 12:34 AM

> Dunno if WinRAR supports [self-extracting archives], but the 7zip program does.


I agree, but when the archive is only 60kb to start with, the overhead of the self-extracting EXE is a little too much to bear ;)

Jeff Atwood on February 22, 2006 01:57 AM

Hi,

I can highly recommend IZArc [http://www.izarc.org/] which is freeware. It uncompresses most formats and can compress to zip, 7-zip and a bunch of others (not rar though).

I like the interface better than WinZip or WinRAR.

regards,
martin

Martin J Steer on February 22, 2006 03:32 AM

Maybe I'm missing something, but for me, installing these snippets using the Microsoft download was easy-peasy. I just added the root folder (in my case, D:\Documents\MSDN\Visual C# 2005 Code Snippets\) in the Code Snippet Manager and everything 'just worked(TM)'. No manually adding each folder for me! Weeee!

Fredrik Meyer on February 22, 2006 04:46 AM

I like 7-Zip just as it is. :-) Sure, there's room for improvements, but it's free and it basically does everything I need it to do.

Anyway, thanks for the "installer". I did download Microsoft's MSI packages a while ago, but I didn't install all of them for the same reason you describe.

C-J Berg on February 22, 2006 11:20 AM

> I just added the root folder (in my case, D:\Documents\MSDN\Visual C# 2005 Code Snippets\) in the Code Snippet Manager

I wanted the snippets in the same folder as the "standard" C# set-- which means you have to add each folder individually.

But you can add an arbitrary new folder, or put the snippets in your user snippet folder.

Jeff Atwood on February 22, 2006 12:22 PM

Interestingly enough, I can not find the "Code Snippets Manager" on my VS2005 Tools menu.

All the MSDN2 Articles just assume it will be there, and I haven't yet figured out how to force it to show up either.

Mort.

Malcolm Anderson on February 22, 2006 01:44 PM

I second Jon Galloway on the Darwinia accusation! Though the code snippets aren't going to save me any time.

Joost on February 22, 2006 02:42 PM

Jeff > Ah, ok. So I _was_ missing something! :)

Malcolm > On one of my VS installations, I had no menu item either, for some reason. I added it myself by... Right-click on the menu, choose Customize, go to the Commands tab, from the Categories list click Tools. In the right-hand list you should see it listed as Code Snippets Manager. Drag-and-drop it from there to wherever you want it in the menu. Works?

Fredrik Meyer on February 22, 2006 02:45 PM

> Interestingly enough, I can not find the "Code Snippets Manager" on my VS2005 Tools menu

Use the keyboard shortcut I mentioned in the post: CTRL+K, CTRL+B

Same issue with Team Developer installed. It doesn't appear in the menus by default. This can of course be customized.. Frederik described how to add it.

Jeff Atwood on February 22, 2006 02:53 PM

After installing the C# code snippets, I have an extra folder that appears in the pop-up as shown in this picture: http://pics.bbzzdd.com/users/mrmilney/snippets_popup.gif

The 1033 folder does not appear in the snippets manager dialog as you can see in this picture: http://pics.bbzzdd.com/users/mrmilney/snippets_manager.gif

I have looked and looked in vain, but I can't see where this extra entry is stored in either the registry or in any configuration files in the VS 2005 directory. I'd appreciate any help anyone could give me.

Joe Paris on March 7, 2006 01:06 AM

I apologize for the bad links in the above post, if anyone wants to see the pics I will be glad to e-mail them to you.

The problem is this: when I type ctrl+k, ctrl+x to bring up the Insert Snippet pop-up in VS.NET 2005, the topmost folder in the list is 1033. If I bring up the Code Snippets Manager with ctrl+k, ctrl+b the 1033 folder is not listed (and therefore I can't remove it).

I have checked both the registry keys referenced in the add-csharp-snippets.reg file, but I can't see where this extra entry is coming from. It seems that VS 2005 is hiding the snippet paths somewhere else, but I can't figure out where. If anyone has any suggestions, I'd love to hear them.

Joe Paris on March 7, 2006 01:18 AM

The registry is the master source, no doubt about it. Check both local and user keys:

HKLM\Software\Microsoft\VisualStudio\8.0\Languages\CodeExpansions\CSharp

HKCU\Software\Microsoft\VisualStudio\8.0\Languages\CodeExpansions\Visual C#

Beware, the path is duplicated within these keys-- check for a "Path" subkey. I have no idea why, but it is.

You can validate this yourself by adding a new path in the snippet manager. If you check the reg key afterwards, you'll see it is there..

Jeff Atwood on March 7, 2006 04:07 AM

It seems that the link for download [http://www.codinghorror.com/blog/files/ms-csharp-snippets.7z.zip]
is dead. Could you update the link.

Murugan.G

Murugan on March 7, 2006 04:38 AM

I agree that the snippet entries _must_ be in the registry, but I can't seem to find the one I need to remove this extra folder. I even went so far as to manually add "%InstallRoot%\VC#\Snippets\%LCID%;" to both path keys under HKCU. While this did make the 1033 entry appear in the Code Snippets Manager, removing it from there did not make the problem go away.

Does anyone have any further suggestions? My OC is kicking in and that extra folder is really bothering me.

Joe Paris on March 7, 2006 12:27 PM

Joe, it's really weird that there is a mismatch between what the snippet manager shows and what shows up in the IDE! I have no idea what's going on there, but triple check all those registry keys and sub-keys.

> Could you update the link.

The link works for me:

http://www.codinghorror.com/blog/files/ms-csharp-snippets.7z.zip

I checked in IE6 and Firefox 1.5.

Jeff Atwood on March 7, 2006 12:37 PM

My 2¢: 7-Zip rocks.


Everyone should be using 7-Zip. But the best way to get everyone using it is to get your friends to install TugZip. TugZip ( http://www.tugzip.com/ ) is the best windows archiver I've ever used. Its integration into the file manager is awesome, and it's completely free!

Not only does it allow right-click creation of archives and unzipping multiple archives at once (each into it's own directory), but it allows you to customize the icons for each type of archive that it supports, and lets you set the default compression level (including creating solid archives) for those right-click "add to .7z" actions.


The only thing it CAN'T do is CREATE rar archives (without rar.exe). But it does unpack them with no problems, which is all I care about, since I'm making 7-Zip, not rar.

Joel "Jaykul" Bennett on March 8, 2006 04:53 PM

For all of you who're seeing folders that aren't there when pressing CTRL-K + CTRL-X, the key is to delete this folder:

C:\Documents and Settings\\Local Settings\Application Data\Microsoft\VisualStudio

Next time you start VS it'll get fixed.

Edgardo on March 20, 2006 01:28 PM

Edgardo...

Brilliant! It worked perfectly. Thank you very much. (If you have a minute, would you mind sharing _how_ you discovered that? I'd really like to know.)

Joe Paris on March 22, 2006 12:46 AM

I simply figured if its not in the registry, where the heck can be stored?

After 30 minutes of inspecting all the appdata/local settings/etc inside my username (you'd be amazed the amount of crap I found--this is a 2-years old install) I found that VS folder so I decided to delete it, and well, it worked. :)

Edgardo on April 5, 2006 02:44 AM

If you really need to know the following file is what contains these "extra" folders:

C:\Documents and Settings\user\Local Settings\Application Data\Microsoft\VisualStudio\8.0\1033\ExpansionsXML.xml

Edit this file to remove the extra ones.

MikeWo on July 10, 2006 09:33 AM

For those of you using Visual Studio Express open the reg file and replace 'VisualStudio' with 'VCSExpress' in each path.

Fin on July 23, 2006 06:00 PM

Thank you all for posting your comments.

They ALL helped me get not only the Snippets working OK but you also helped me understand the whole VS2005 Dev IDE a little bit better.

One thing though - when you are looking for a solution to a problem that shouldn't be a problem (and boy this definitely falls into this category) there is nothing more that pisses me of than having to instal another proggy just to get the original problem solved.

Until now I'd never heard of 7.thingErMeJig and until I have a lot of spare time I probaby won't again.

Once again, sincere thanks to you all...

glenharvy on July 25, 2006 10:15 PM

joe paris, i was having the same issue as you were. their decision for microsoft to do it like that was asinine.

however, thanks to Edgardo and MikeWo for your replies to this thread. it was driving me crazy, but now i'm where i need to be.

THANKS!

-
jen

jen on October 10, 2006 08:10 AM

That was really cool. Especially finding the Code snippet manager helped me a lot! I was shocked when I didn't find it under my tools menu!
Best of luck

Alireza on March 19, 2007 12:56 AM

Thanks for this code snipped.It helped me a lot!
Pefix from Sicily!!

pefix on March 29, 2007 10:55 AM

code snippets are coooool!

www.hidden-geek.co.cc

Geek_2k on April 15, 2008 09:11 AM







(hear it spoken)


(no HTML)




Content (c) 2008 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.