Updated: August 2011
Converts the string representation of a GUID to the equivalent Guid structure.
Assembly: mscorlib (in mscorlib.dll)
Public Shared Function Parse ( _ input As String _ ) As Guid
public static Guid Parse( string input )
public: static Guid Parse( String^ input )
static member Parse : input:string -> Guid
Parameters
- input
- Type: System.String
The GUID to convert.
| Exception | Condition |
|---|---|
| ArgumentNullException |
input is null. |
| FormatException |
input is not in a recognized format. |
Use the TryParse method to catch any unsuccessful parse operations.
The following example creates a new GUID, converts it to three separate string representations by calling the ToString method with the "B", "D", and "X" format specifiers, and then calls the Parse method to convert the strings back to Guid values.
Module Example Public Sub Main() Dim originalGuid As Guid = Guid.NewGuid() ' Create an array of string representations of the GUID. Dim stringGuids() As String = { originalGuid.ToString("B"), originalGuid.ToString("D"), originalGuid.ToString("X") } ' Parse each string representation. For Each stringGuid In stringGuids Try Dim newGuid As Guid = Guid.Parse(stringGuid) Console.WriteLine("Converted {0} to a Guid", 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: ' 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
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. foreach (var stringGuid in stringGuids) { try { Guid newGuid = Guid.Parse(stringGuid); Console.WriteLine("Converted {0} to a Guid", stringGuid); } catch (ArgumentNullException) { Console.WriteLine("The string to be parsed is null."); } catch (FormatException) { Console.WriteLine("Bad format: {0}", 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
.NET Framework
Supported in: 4.NET Framework Client Profile
Supported in: 4Windows 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.
Reference
|
Date |
History |
Reason |
|---|---|---|
|
August 2011 |
Replaced the example. |
Information enhancement. |
|
July 2011 |
Removed Exception from the list of exceptions. |
Content bug fix. |