C# - SPLessons

C# how to create a Guid value

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# how to create a Guid value

Description :
Hello Everyone, In my last post we have seen Creating a Site Column in SharePoint programmatically . If you observe in that post we have used GUID(Unique Reference) for each of the Column. In our current post we are going to see in C# how to create a Guid value Programmatically. For this i am just writing one Console and adding all GUID to the text file.

GUID :
A GUID (Global Unique Identifier ) is a term used by Microsoft for a number that its programming generates to create a unique identity for an entity

Step1 :
Open Visual Studio.

Step2 :
Create one Empty Console Application.

Step3 :
paste the below code in Program.cs

[c-sharp] class Program { static void Main(string[] args) { try { System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\testGuID.txt"); for (int i = 0; i < 10; i++) { Guid guId = new Guid(); guId = Guid.NewGuid(); file.WriteLine(guId); //Console.WriteLine(guId.ToString()); } file.Close(); Console.ReadLine(); } catch (Exception ex) { // exception handling } } } [/c-sharp]

Step4 :
Just deploy the solution and navigate to your physical path, GUID will be generated. Note: In our case i have given 10 as limit, you can change if required in your case. Happy Coding :-)