Share via


Guid.ParseExact Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function ParseExact ( _
    input As String, _
    format As String _
) As Guid
public static Guid ParseExact(
    string input,
    string format
)

Parameters

  • 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.

Exceptions

Exception Condition
ArgumentNullException

input or format is nulla null reference (Nothing in Visual Basic).

FormatException

input is not in a recognized format.

Remarks

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}}

Examples

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.

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      ' Define an array of all format specifiers.
      Dim formats() As String = { "N", "D", "B", "P", "X" }
      Dim guid As Guid = Guid.NewGuid()
      ' Create an array of valid Guid string representations.
      Dim stringGuids(formats.Length - 1) As String
      For ctr As Integer = 0 To formats.Length - 1
         stringGuids(ctr) = guid.ToString(formats(ctr))
      Next

      ' Parse the strings in the array using the "B" format specifier.
      For Each stringGuid In stringGuids
         Try
            Dim newGuid As Guid = Guid.ParseExact(stringGuid, "B")
            outputBlock.Text += String.Format("Successfully parsed {0}", stringGuid) + vbCrLf
         Catch e As ArgumentNullException
            outputBlock.Text += "The string to be parsed is null." + vbCrLf
         Catch e As FormatException
            outputBlock.Text += String.Format("Bad Format: {0}", stringGuid) + vbCrLf
         End Try   
      Next      
   End Sub
End Module
' The example displays the following output:
'    Bad Format: 3351d3f0006747089ff928b5179b2051
'    Bad Format: 3351d3f0-0067-4708-9ff9-28b5179b2051
'    Successfully parsed {3351d3f0-0067-4708-9ff9-28b5179b2051}
'    Bad Format: (3351d3f0-0067-4708-9ff9-28b5179b2051)
'    Bad Format: {0x3351d3f0,0x0067,0x4708,{0x9f,0xf9,0x28,0xb5,0x17,0x9b,0x20,0x51}}
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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");
            outputBlock.Text += String.Format("Successfully parsed {0}\n", stringGuid);
         }   
         catch (ArgumentNullException) { 
            outputBlock.Text += "The string to be parsed is null.\n";
         }   
         catch (FormatException) {
            outputBlock.Text += String.Format("Bad Format: {0}\n", 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}}

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.