Tags: , | Categories: Programming, Visual Studio Posted on 6/16/2009 7:48 AM | Comments (0)

I have often had the need for insert GUIDs in my code, but the existing guid generation tool that comes with Visual Studio is quite the drag to use (in my opinion). I mean it works and everything, but this is the steps necessary to generate and insert a guid in the code:

  1. Open the "Tools" menu
  2. Click on "Create GUID" and wait for it to start
  3. (optional) Select the GUID format
  4. Click on "Copy"
  5. Click on "Exit"
  6. Ctrl+V to insert in the code.

At least it generates a new guid every time it opens. Granted I could let the tool remain open, and just click "New GUID" but I don't need GUIDs THAT often. So, all in all, quite the drag in my opinion. Enter macros.

I realized that I could accomplish all of the above in a single hotkey combination by writing a macro that does this exact thing.Says alot about me, that I used VS for 4 years before this occured to me...

See here:

 

   1: Public Sub InsertGUID()
   2:     Dim guid As System.Guid = System.Guid.NewGuid()
   3:     Dim ts As TextSelection = DTE.ActiveWindow.Selection
   4:     ts.Insert(guid.ToString().ToUpper())
   5: End Sub

 

Then I create a hotkey for it in Options –> Environment –> Keyboard. And voila! Every time I press Ctrl+Shift+G I now generate a new GUID and insert it into the code at the exact position the cursor is at.