This documentation is archived and is not being maintained.

Guid.TryParseExact 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 Shared Function TryParseExact ( _
	input As String, _
	format As String, _
	<OutAttribute> ByRef result As Guid _
) As Boolean


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

Return Value

Type: System.Boolean
true if the parse operation was successful; otherwise, false.

This method returns false if input is Nothing or not in a recognized format, and does not throw an exception.

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


Format of the input parameter


32 digits:



32 digits separated by hyphens:



32 digits separated by hyphens, enclosed in braces:



32 digits separated by hyphens, enclosed in parentheses:



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


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 TryParseExact method, which successfully parses the string that conforms to the "B" format specifier.

Module Example
   Public Sub Main()
      ' 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))

      ' Try to parse the strings in the array using the "B" format specifier.
      For Each stringGuid In stringGuids
         Dim newGuid As Guid
         If Guid.TryParseExact(stringGuid, "B", newGuid) Then
            Console.WriteLine("Successfully parsed {0}", stringGuid)
            Console.WriteLine("Unable to parse '{0}'", stringGuid)
         End If   
   End Sub
End Module
' The example displays the following output:
'    Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
'    Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
'    Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
'    Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
'    Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'

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




August 2011

Replaced the example.

Information enhancement.