Path.GetExtension Path.GetExtension Path.GetExtension Path.GetExtension Method

Definition

Overloads

GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>)
GetExtension(String) GetExtension(String) GetExtension(String) GetExtension(String)

Returns the extension of the specified path string.

GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>) GetExtension(ReadOnlySpan<Char>)

public:
 static ReadOnlySpan<char> GetExtension(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetExtension (ReadOnlySpan<char> path);
static member GetExtension : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetExtension (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)
Parameters
Returns

GetExtension(String) GetExtension(String) GetExtension(String) GetExtension(String)

Returns the extension of the specified path string.

public:
 static System::String ^ GetExtension(System::String ^ path);
public static string GetExtension (string path);
static member GetExtension : string -> string
Public Shared Function GetExtension (path As String) As String
Parameters
path
String String String String

The path string from which to get the extension.

Returns

The extension of the specified path (including the period "."), or null, or Empty. If path is null, GetExtension(String) returns null. If path does not have extension information, GetExtension(String) returns Empty.

Exceptions

path contains one or more of the invalid characters defined in GetInvalidPathChars().

Examples

The following code example demonstrates using the GetExtension method on a Windows-based desktop platform.

String^ fileName = "C:\\mydir.old\\myfile.ext";
String^ path = "C:\\mydir.old\\";
String^ extension;
extension = Path::GetExtension( fileName );
Console::WriteLine( "GetExtension('{0}') returns '{1}'", fileName, extension );
extension = Path::GetExtension( path );
Console::WriteLine( "GetExtension('{0}') returns '{1}'", path, extension );

// This code produces output similar to the following:
//
// GetExtension('C:\mydir.old\myfile.ext') returns '.ext'
// GetExtension('C:\mydir.old\') returns ''
string fileName = @"C:\mydir.old\myfile.ext";
string path = @"C:\mydir.old\";
string extension;

extension = Path.GetExtension(fileName);
Console.WriteLine("GetExtension('{0}') returns '{1}'", 
    fileName, extension);

extension = Path.GetExtension(path);
Console.WriteLine("GetExtension('{0}') returns '{1}'", 
    path, extension);

// This code produces output similar to the following:
//
// GetExtension('C:\mydir.old\myfile.ext') returns '.ext'
// GetExtension('C:\mydir.old\') returns ''
Dim fileName As String = "C:\mydir.old\myfile.ext"
Dim pathname As String = "C:\mydir.old\"
Dim extension As String

extension = Path.GetExtension(fileName)
Console.WriteLine("GetExtension('{0}') returns '{1}'", fileName, extension)

extension = Path.GetExtension(pathname)
Console.WriteLine("GetExtension('{0}') returns '{1}'", pathname, extension)

' This code produces output similar to the following:
'
' GetExtension('C:\mydir.old\myfile.ext') returns '.ext'
' GetExtension('C:\mydir.old\') returns ''

Remarks

The extension of path is obtained by searching path for a period (.), starting with the last character in path and continuing toward the start of path. If a period is found before a DirectorySeparatorChar or AltDirectorySeparatorChar character, the returned string contains the period and the characters after it; otherwise, Empty is returned.

For a list of common I/O tasks, see Common I/O Tasks.

See Also

Applies to