Generating a plain GUID in Visual Studio
Asked Answered
R

6

21

Create GUID box

If I try using the "Create GUID" tool using "Tool" > "Create GUID", I get a box that has six different snippets which contains GUIDs. But it doesn't offer the option of providing only the GUID. Is there any way to do this?

I'm using Visual Studio Professional 2019, version 16.1.6, and .NET version 4.8.03761.

Raleighraley answered 30/10, 2019 at 23:2 Comment(1)
You can find alternative ways from posts like kristian.hellang.com/generating-guidsBehave
A
2

You can use the following command (language C#) for my Visual Commander extension to insert a plain GUID:

public class C : VisualCommanderExt.ICommand
{
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
    {
        EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
        ts.Text = System.Guid.NewGuid().ToString();
    }
}
Alyce answered 31/10, 2019 at 4:4 Comment(0)
K
36

Instead of installing a new extension (as mentioned in the other answers), you can just use a built-in tool in Visual Studio called "C# Interactive".

Here are the steps:

  1. On VS open the "C# Interactive" window (if not opened) by
    pressing Ctrl+Q and typing "C# Interactive"
  2. Type the following in the window and press enter:
    System.Guid.NewGuid().ToString()

And you'll get the result:

enter image description here

Note: In case you need it again, just hit Up Arrow button and the last command will reappear, so you don't need to type again.


P.S. Basically we just execute a C# code in the "C# Interactive" window.
You can modify the code there to whatever you want. For example, you can go further and generate a list of guids with by typing there a code like this:

> for (int i = 0; i < 100; i++)
{
    Console.WriteLine(Guid.NewGuid().ToString());
}
Kibosh answered 17/2, 2021 at 9:40 Comment(1)
quiet complicated dont you think...Tools/Create GUID seams to be way easierMethylene
L
16

The Create UID option is available by default in Professional and Enterprise versions. To have Create GUID functionality you can add the Create GUID tool yourself by following the below steps.

  1. Go to the Visual Studio menu and click on Tools then External Tools.

    Visual Studio Tools

  2. The below model will pop up.

    New Tool Popup Modal

    Title: Create GUID
    Command: Powershell.exe
    Arguments: [guid]::NewGuid()
    Use Output Window: Check/True 
    
  3. Click Apply then OK.

  4. Now again go to VS Menu -> Tools and you'll be able to see the Create GUID option in the Tools menu.

    Create GUID Option

  5. Click on Create GUID and check the output window.

    Generated GUID

Louannlouanna answered 12/11, 2021 at 12:43 Comment(4)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewVernievernier
Dear these links are Images I Uploaded not allowed to post images as I am new to stackoverflow.Louannlouanna
Can this generate multiple Guids in one action? Like it promts for how many Guids you want, and then it will print them via powershell? Is there a way to do that?Whaling
Use this for uppercase GUID: [guid]::NewGuid().toString().ToUpper()Choppy
A
2

You can use the following command (language C#) for my Visual Commander extension to insert a plain GUID:

public class C : VisualCommanderExt.ICommand
{
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
    {
        EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
        ts.Text = System.Guid.NewGuid().ToString();
    }
}
Alyce answered 31/10, 2019 at 4:4 Comment(0)
S
2

Go to Tools > External Tools

enter image description here

Title: Create &GUID

Visual Studio 2022

Command: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\guidgen.exe

Initial Directory: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools

Visual Studio 2019

Command: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\guidgen.exe

Initial Directory: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools

It is possible that your directories may be different. Just search for "guidgen.exe" if you have issues finding it.

Now you can go Tools > Create Guid and see:

enter image description here

Skeg answered 8/3, 2022 at 20:53 Comment(1)
Any reason why you use the & sign in the title? I think the title value is optional.Whaling
L
1

Install this extension: Insert Guid by Mads Kristensen (works with Visual Studio 2017, 2019, 2022).

And you will have a new entry in the Edit menu (with the shortcut Ctrl + K, Ctrl + Space):

enter image description here

That will insert a GUID exactly in the point you are in the code.

Lankford answered 14/4, 2022 at 3:6 Comment(2)
As long as you have this extension installed: Insert Guid by Mads KristensenRoundel
Thanks Paul, I missed that.Lankford
H
0

Adding my version of the External Tool solution. This version is based on @MMUNEEBALI solution but extends it by copying the GUID to the clipboard.

  • Title: NEW GUID (Clipboard)
  • Command: Powershell.exe
  • Arguments: $guid = (New-Guid).Guid; Set-Clipboard $guid; Write-Host New GUID [$guid] copied!
  • Use Output Window: Check/True
  • Close on exit: Check/True
Hoodwink answered 21/8, 2023 at 6:50 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Malonylurea

© 2022 - 2024 — McMap. All rights reserved.