How to create a GUID in C#.Net [With Video Tutorial]

In this C#.Net tutorial, we will discuss how to create a GUID in C#.Net programmatically as well as using Visual Studio 2017/2019/2015.

Sometimes you may need to pass a GUID to a function or maybe sometimes, you need to create a GUID and pass the GUID at runtime.

Here I will show you both the ways how to create a GUID in C#.Net.

Video Tutorial: Create GUID programmatically using C#

I have also created a video tutorial on how to create a GUID programmatically using C#. This video tutorial explains different ways to create a GUID in C#.Net. Learn how to create a GUID from Visual Studio, how to Create GUID programmatically using C#.Net. how to generate GUID without a hyphen in c#.net, how to c# generate guid with a specific length. How to generate guid in asp.net c#.net? And how to generate guid from a string in c#.

Create GUID from Visual Studio

Now, we will see how we can create a GUID using visual studio (any version).

Open Visual Studio and then click on Tools -> Create GUID like below:

create guid in c#
c# new guid

This will open the Create GUID dialog box where you can create different types of GUIDs. You can use the Copy, New GUID buttons to copy or create a new GUID.

create guid in c#.net
c# new guid

Create GUID programmatically using C#.Net

Now we will see how to create a GUID programmatically using C#.Net using the System.GUID class.

For this, you can create any application, but for this particular example, I have created a Windows application.

To create a Windows Application using visual studio, open your visual studio 2017, File -> New -> Project.

Then select Windows Forms App (.NET Framework) which is under Visual C# -> Windows Desktop.

In the windows application, I have added a button and a label. On the button click, we are generating the GUID and displaying in the label.

Below is the code to create GUID which I have added in the button click.

private void btnCreateGUID_Click(object sender, EventArgs e)
        {
            Guid guid = Guid.NewGuid();
            lblResult.Text = "New GUID Is: " + guid.ToString();
        }

Once you run the application and click on the button, it will display the GUID in the label like below:

generate random guid c#
c# new guid

Does your GUID is generating like {00000000-0000-0000-0000-000000000000}?

If your GUID is generating like below:
{00000000-0000-0000-0000-000000000000}

how to create unique guid in c#
c# new guid using Guid class

Then check your code, you must have been written like below:

Guid guid = new Guid();

Replace the above line with the below line.

Guid guid = Guid.NewGuid();

generate GUID without hyphen

We can also create a GUID without a hyphen. You can modify the code like below:

private void btnCreateGUID_Click(object sender, EventArgs e)
        {
Guid guid = Guid.NewGuid();
lblResult.Text = "New GUID Is: " + guid.ToString("N");
}

Here, I have just used the “N” format, there are also, other formats we can use like D, B, P, X, you can check this MSDN article for more information.

Or you can just do a string replace method, to replace the hyphen with empty like below:

private void btnCreateGUID_Click(object sender, EventArgs e)
        {
            Guid guid = Guid.NewGuid();
            lblResult.Text = "New GUID Is: " + guid.ToString().Replace("-", string.Empty);
        }

Now, your GUID without a hyphen will appear like below:

generate GUID without hyphen
c# new guid

This is how we can create a Guid without dashes Using C#.Net.

c# generate guid with specific length

Sometimes, you may get to generate a GUID with a specific length. Suppose you want to send a random password (15 character length), then you can generate the GUID like below:

private void btnCreateGUID_Click(object sender, EventArgs e)
        {
            Guid guid = Guid.NewGuid();
            lblResult.Text = "New GUID Is: " + guid.ToString("N").Substring(0, 15);
        }

Once you click on the button, it will display a random GUID of 15 character length.

c# generate guid with specific length
c# new guid with specific length

generate guid in asp.net c#

If you are getting any requirement to generate guid in asp.net c#, then you can use the same Guid class to generate the guid programmatically as below:

Guid guid = Guid.NewGuid();
string newguid=guid.ToString();

The only difference is that we are writing the code inside an asp.net webform application.

C# generate guid from string

We can also create or generate guid from string by using MD5 Class from Microsoft.

The ComputeHash method of MD5 will take a string and returns the hash as a 32-character, hexadecimal-formatted string.

Below is the full code to generate guid from string in c#.

Make sure to write the below, using statement.

using System.Security.Cryptography;
private void btnCreateGUID_Click(object sender, EventArgs e)
        {
            string mystring = "BijayKumar";
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(mystring));
            Guid guid = new Guid(data);
            lblResult.Text = guid.ToString();
        }

The output will look like below:

c# generate guid from string
c# new guid

You can check out the following C#.net tutorials:

This C#.Net, we learned how to create a GUID using visual studio. Also, we saw how to generate a GUID programmatically using C#.Net.

Also, we saw how to if the GUID is generating in {00000000-0000-0000-0000-000000000000} format. Also, we saw how to generate GUID without a hyphen and how to generate guid with specific length in C#.Net?