Is it possible to create a code snippet or something similar to automate the process of generating and inserting GUIDs in to the text editor in Visual Studio 2012? I frequently need to generate new GUIDs (WiX installer for example, as well as our own internal framework).
I used to use a macro to perform this job, creating a new GUID and then inserting it in to the current ActiveDocument. I would the bind the macro to Shift-Ctrl G so I could rapidly insert a new ID without having to launch the Create Guid tool and copy from there.
Macro functionality has now been removed from Visual Studio 2012 so I need an alternative method, I would assume that it is possible to do with an Extension but I am unfamiliar with developing extensions and that approach would seem a little heavy handed!
Any suggestions would be appreciated, failing that then a pointer at any information on what sort of extension would be required to interact with the text window and insert text would be appreciated. I could then make an extension and post it on the Visual Studio Gallery.
Thanks, Anthony
Edit to add - whatever solution is proposed would need to be "triggerable" from a keyboard shortcut. As I stated above, I tied the macro to Ctrl Shift G because it was easy to remember and press while writing code.
Insert a new GUID by invoking the command under the Edit top-level menu or hit CTRL+K,Space . If Visual Studio is unable to insert the GUID, it will be copied to the clipboard so you can easily paste it in manually. When a GUID is copied to the clipboard, a notification will be displayed in the status bar.
A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.
C# Guid. A GUID (Global Unique IDentifier) is a 128-bit integer used as a unique identifier.
ReSharper allows you to insert a new guid by typing "nguid" and pressing tab.
Obviously this is a tad on the expensive side just for the ability to generate a Guid however ReSharper has many other useful features that might be worth considering.
You could write a Visual Studio 2012 extension to accomplish this!
If you've never written an Add-in before, this is a simple one to get you started!
Here are the steps to create this type of add-in:
Finish the wizard and implement the follow code in Exec(...)
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { handled = false; if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { if (commandName == this.GetType().Namespace + ".Connect." + this.GetType().Namespace) { if (_applicationObject.ActiveDocument != null) { TextSelection objSel = (EnvDTE.TextSelection)(_applicationObject.ActiveDocument.Selection); objSel.Insert(Guid.NewGuid().ToString()); } handled = true; return; } } }
Build the project, and deploy AddInName.dll, AddInName.AddIn, and AddInName.xml to c:\users\username\documents\Visual Studio 2012\Addins. [If the Addins folder doesn't exist, create it]
Voila! Hotkey GUID generation and a little bit of Visual Studio Add-in know how. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With