Generating GUIDs in the Visual Studio IDE

If you’re programming on Windows you’ll often come across a situation where you have to generate a new GUID. In fact I find myself generating new GUIDs more than ever! I used to use them only for COM/ActiveX and Windows shell integration stuff. Nowadays however I spit out several GUIDs a day, especially for projects involving WiX and SharePoint. In fact, I tend to use them everytime I need a unique identifier. Here’s one I’ve generated especially for this blog post:

{1B87C5E2-0818-4c21-ADFC-741E8D8C1B3D} 😉

The classic tool of choice for generating GUIDs is ofcourse GuidGen.exe, which ships with all the major Microsoft SDK’s and can also be started from the Tools menu of Visual Studio. GuidGen is a small tool which gets the job done, but somehow I have never gotten used to it. I don’t like the fact that I have to leave the Visual Studio IDE to use it and that I cannot control the output format of the GUIDs GuidGen generates. For instance when using WiX you need to use GUIDs without the accolades, which means that a GUID generated by GuidGen needs some editing before it can be used. Yawn…

In the past I was a pretty avid Borland Delphi programmer. One of the nice things of the Delphi IDE, and one that I used very often, was that you could press the keyboard shortcut <ctrl> + <alt> + G and it would place a fresh new GUID at the cursor’s position in the source code editor. I really missed this functionality in Visual Studio.

Fortunately Visual Studio can be easily customized using macro’s and it was easy hacking together a little macro that does the job. I’ve used it for several years now and shared it with many colleagues and friends. Here it is:

Sub Create_GUID()
    Dim sel As TextSelection
    sel = DTE.ActiveDocument.Selection
    sel.Text = System.Guid.NewGuid.ToString("D").ToUpper()
End Sub

Just add this macro to Visual Studio and assign it to the keyboard shortcut of your choice. I use <alt> + G, since the original Delphi shortcut I mentioned above is already in use by Visual Studio by default (it point to the Debug.Registers function).

Ofcourse you can easily modify this macro to suit your personal needs. If you like the way GuidGen bakes ’em, here it is:

Sub Create_GUID
    Dim sel As TextSelection
    sel = DTE.ActiveDocument.Selection
    sel.Text = "{" + System.Guid.NewGuid.ToString("D").ToUpper() + "}"
End Sub

Or maybe you prefer underscores instead of hyphens:

Sub Create_GUID
    Dim sel As TextSelection
    sel = DTE.ActiveDocument.Selection
    sel.Text = Replace(System.Guid.NewGuid.ToString("D").ToUpper(), "-", "_")
End Sub

Or you might be a lower-case fanatic:

Sub Create_GUID
    Dim sel As TextSelection
    sel = DTE.ActiveDocument.Selection
    sel.Text = System.Guid.NewGuid.ToString("D").ToLower()
End Sub

Anyway, I think you get the point…

Posted on November 3, 2007, in developing, microsoft, sharepoint, wix and tagged , , , , , . Bookmark the permalink. 11 Comments.

  1. You can also use a * instead of a guid. Wix will generate one himself.

  2. @Rolf Huisman: Yes, I know that WiX nowadays has support for auto-generating GUIDs on certain elements. I myself sometimes use it for generating Package IDs, which should be unique for each build. But in general I consider automatically generated GUIDs evil. It’s easy to break you setups by using them in the wrong place.

  3. I use an online GUID generator when I need GUIDs since I couldn’t get the guidgen thing to work in VS 2008 – http://createguid.com

  4. @SharePointer: I’ve included an updated version of the macro that works for Visual Studio 2008 at the end of the article.

  5. I’ve updated the macro source code in this article, so it is compatible with Visual Studio 2005/2008.

  6. Wow, this is great! Much handier than GuidGen. Thank you very much!

    Lars

  7. U Da man – helped me much !!!

    thx

  8. This is a life-changing macro! Thanks!

  1. Pingback: Quickly Generating GUIDs in Visual Studio 2008 « Point Deep

  2. Pingback: Generating GUIDs from Visual Studio 2008 | David's Blog

  3. Pingback: One-Stroke GUIDs in Visual Studio | James Reuben Knowles

Leave a reply to Lars Cancel reply