Guid.TryParse Method
Converts the string representation of a GUID to the equivalent Guid structure.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- input
- Type: System.String
The GUID to convert.
- result
- Type: System.Guid
The structure that will contain the parsed value. If the method returns true, result contains a valid Guid. If the method returns false, result equals Guid.Empty.
The following example creates a new GUID, converts it to three separate string representations by calling the ToString(String) method with the "B", "D", and "X" format specifiers, and then calls the TryParse method to convert the strings back to Guid values.
using System; public class Example { public static void Main() { Guid originalGuid = Guid.NewGuid(); // Create an array of string representations of the GUID. string[] stringGuids = { originalGuid.ToString("B"), originalGuid.ToString("D"), originalGuid.ToString("X") }; // Parse each string representation. Guid newGuid; foreach (var stringGuid in stringGuids) { if (Guid.TryParse(stringGuid, out newGuid)) Console.WriteLine("Converted {0} to a Guid", stringGuid); else Console.WriteLine("Unable to convert {0} to a Guid", stringGuid); } } } // The example displays the following output: // Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid // Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid // Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.