3 out of 7 rated this helpful - Rate this topic

Guid.ToString Method (String)

Updated: January 2010

Returns a String representation of the value of this Guid instance, according to the provided format specifier.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public string ToString(
	string format
)

Parameters

format
Type: System.String
A single format specifier that indicates how to format the value of this Guid. The format parameter can be "N", "D", "B", or "P". If format is null or the empty string (""), "D" is used.

Return Value

Type: System.String
The value of this Guid, represented as a series of lowercase hexadecimal digits in the specified format.
Exception Condition
FormatException

The value of format is not null, the empty string (""), "N", "D", "B", or "P".

The format parameter can contain the following format specifiers. In the table that follows, all digits in the return value are hexadecimal. Each character 'x' represents a hexadecimal digit; each hyphen ('-'), bracket ('{', '}'), and parenthesis ('(', ')') appears as shown.

Specifier

Format of Return Value

N

32 digits:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

D

32 digits separated by hyphens:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

B

32 digits separated by hyphens, enclosed in brackets:

{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

P

32 digits separated by hyphens, enclosed in parentheses:

(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

January 2010

Updated return value description.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Sample using PowerShell
  

<#
.SYNOPSIS
This script Creates then displays a GUID by using .TOString()
.DESCRIPTION
This script demonstrates the 4 format specifiers you can
send to .TOString() to affect the display. Note when you run
this script, you will get a different GUID to the one shown
in the example
.NOTES
File Name : Format-GUID.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:

http://www.pshscripts.blogspot.com
MSDN Sample posted at:


http://msdn.microsoft.com/en-us/library/97af8hh4.aspx
.EXAMPLE
PS Mod:\> E:\PowerShellScriptLib\System.Guid\Format-GUID.ps1
Using the P format string, guid is: (f221065b-a57d-42ac-aa1d-0bfeb433c804)
Using the D format string, guid is: f221065b-a57d-42ac-aa1d-0bfeb433c804
Using the N format string, guid is: f221065ba57d42acaa1d0bfeb433c804
Using the B format string, guid is: {f221065b-a57d-42ac-aa1d-0bfeb433c804}
#>

##
# Start of Script
##

# Create a GUID
$guid =[system.guid]::newguid()


# Display GUID
# Using the P secifier
"Using the {0} format string, guid is: {1}" -f "P",$guid.tostring("P")






# Using the D secifier
"Using the {0} format string, guid is: {1}" -f "D",$guid.tostring("D")



# Using the N secifier
"Using the {0} format string, guid is: {1}" -f "N",$guid.tostring("N")




# Using the B secifier
"Using the {0} format string, guid is: {1}" -f "B",$guid.tostring("B")
# End of script