Guid.ParseExact Method (String, String)
Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.
Assembly: mscorlib (in mscorlib.dll)
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".
| Exception | Condition |
|---|---|
| ArgumentNullException | input or format is null. |
| FormatException | input is not in the format specified by format. |
The ParseExact method requires the string to convert to be exactly in the format specified by the format parameter, after leading and trailing white-space characters are removed. 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(String) 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 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)) 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") Console.WriteLine("Successfully parsed {0}", stringGuid) Catch e As ArgumentNullException Console.WriteLine("The string to be parsed is null.") Catch e As FormatException Console.WriteLine("Bad Format: {0}", stringGuid) 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}}
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1