Kwizcom Forms

How to create a GUID in C#.Net

create guid in c#

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.

SharePoint deveopment training course

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

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#

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

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#

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#

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

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

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?

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →