Guid.ParseExact Method

Updated: August 2011

Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static Guid ParseExact(
	string input,
	string format
)

Parameters

input
Type: System.String
The GUID to convert.
format
Type: System.String
One of the following specifiers that indicates the exact format to use when interpreting input: "N", "D", "B", "P", or "X".

Return Value

Type: System.Guid
A structure that contains the value that was parsed.

ExceptionCondition
ArgumentNullException

input or format is null.

FormatException

input is not in a recognized format.

The following table shows the accepted format specifiers for the format parameter. "0" represents a digit; hyphens ("-"), braces ("{", "}"), and parentheses ("(", ")") appear as shown.

Specifier

Format of the input parameter

N

32 digits:

00000000000000000000000000000000

D

32 digits separated by hyphens:

00000000-0000-0000-0000-000000000000

B

32 digits separated by hyphens, enclosed in braces:

{00000000-0000-0000-0000-000000000000}

P

32 digits separated by hyphens, enclosed in parentheses:

(00000000-0000-0000-0000-000000000000)

X

Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces:

{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

The following example calls the ToString method with each of the supported format specifiers to generate an array of strings that represent a single GUID. These are then passed to the ParseExact method, which successfully parses only the string that conforms to the "B" format specifier.


using System;

public class Example
{
   public static void Main()
   {
      // Define an array of all format specifiers.
      string[] formats = { "N", "D", "B", "P", "X" };
      Guid guid = Guid.NewGuid();
      // Create an array of valid Guid string representations.
      string[] stringGuids = new string[formats.Length];
      for (int ctr = 0; ctr < formats.Length; ctr++)
         stringGuids[ctr] = guid.ToString(formats[ctr]);

      // Parse the strings in the array using the "B" format specifier.
      foreach (var stringGuid in stringGuids) {
         try {
            Guid newGuid = Guid.ParseExact(stringGuid, "B");
            Console.WriteLine("Successfully parsed {0}", stringGuid);
         }   
         catch (ArgumentNullException) { 
            Console.WriteLine("The string to be parsed is null.");
         }   
         catch (FormatException) {
            Console.WriteLine("Bad Format: {0}", stringGuid);
         }   
      }     
   }
}
// The example displays the following output:
//    Bad Format: eb5c8c7d187a44e68afb81e854c39457
//    Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457
//    Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457}
//    Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457)
//    Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}}


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

August 2011

Added an example.

Information enhancement.

July 2011

Removed Exception from the list of exceptions.

Content bug fix.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft