Export (0) Print
Expand All

Guid.TryParseExact Method

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)

public static bool TryParseExact(
	string input,
	string format,
	out Guid result
)

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".
result
Type: System.Guid%
When this method returns, contains the value that is equivalent to the GUID contained in input if the conversion succeeded, or Guid.Empty if the conversion failed.

Return Value

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

This method returns false if input is null 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.

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


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) {
         Guid newGuid;
         if (Guid.TryParseExact(stringGuid, "B", out newGuid))
            outputBlock.Text += String.Format("Successfully parsed {0}\n", stringGuid);
         else 
            outputBlock.Text += String.Format("Unable to parse '{0}'\n", stringGuid);
      }     
   }
}
// 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}}'


Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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

Community Additions

ADD
Show:
© 2014 Microsoft