Snippet paths and duplicates in VS 2008

Posted Wed, Nov 21 2007 11:07 by bill

If you install VS 2008 on a machine that has VS 2005 on it, you'll be prompted to let VS 2008 import as many of your settings from 2005 as possible.  Generally this is a good thing, but you might run into some issues with code snippets as you might have both the 2005 and 2008 collections loaded in VS 2008. 

The problem is Visual Studio stores snippets by default in the %InstallRoot%, which for VS 2005 is Program Files\Microsoft Visual Studio 8 and for VS 2008 it's Program Files\Microsoft Visual Studio 9.0. But when Visual Studio migrates your settings from 2005 to 2008, it just copies the registry value which include %InstallRoot% in the paths as a replacement variable.  So what this means is if you modified the snippets by hand or using some other tool, your new snippets will NOT include the ones you previously modified.

If on the other hand you used the snippet editor in 2005, it saves the paths as the full path, so in VS 2008 you'll have both the 2005 paths and the 2008 paths.  This also is not ideal.  You can quickly remove the root folders you don't want by right clicking on them and selecting remove. (Note: root level folders are the folders stored in the registry and have a blue asterisk on them in the snippet editor)

In either scenario you are still faced with maintaining multiple collections of snippets by default.  The best thing you can do is to create a folder in your user do documents folder, and then move sub folders and snippets to that path. This makes it easy to share snippets between versions of Visual Studio.  You'll need to open the collections to remove the paths that are in the %InstallRoot%, but once done you have a centralized common folder tree for your snippets.

In fact, I like the idea of doing that so much, I think I will add a wizard to the Snippet Editor that does this for you.  This would also let you have your snippets where you don't need administer rights to modify them.

There's also a couple of other minor changes I plan to make later this week, such as surfacing the collection selection onto the folder pane.

Oh, and if you are using the Snippet Editor as is with VS 2008 alongside VS 2005, you might be seeing duplicates for snippets in Visual Studio but not in the Snippet Editor.  This is yet another one of the undocumented "quirks" about how Visual Studio deals with snippets. If you look in the languages\CodeExpansions key registry you'll probably see some values stored under "Basic" and others under "Visual Basic".  This is a "temporary" state.  If you run Visual Studio's Snippet Manager and click on the OK, it will remove the "Basic" key and put all the values under the "Visual Basic" key, at which point the Snippet Editor and VS's Snippet Manager will all be showing the same collection.  When I wrote the Snippet Editor, once the "Visual Basic" key was established in the registry, it meant that the collection was already initialised. If it wasn't there then I'd initialise it for you. Same kind of thing with "CSharp" and "Visual C#".  But now with 2008 that pattern has changed, as in both keys can be there.

Unfortunately little of this if any is documented, so it means my code has to be reactionary rather than pro-active: That is, I have to fix it when they do things that break it ;)

Filed under: , ,