Path.GetExtension Method

Definition

Overloads

GetExtension(ReadOnlySpan<Char>)

Returns the extension of a file path that is represented by a read-only character span.

GetExtension(String)

Returns the extension (including the period ".") of the specified path string.

GetExtension(ReadOnlySpan<Char>)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

Returns the extension of a file path that is represented by a read-only character span.

C#
public static ReadOnlySpan<char> GetExtension(ReadOnlySpan<char> path);

Parameters

path
ReadOnlySpan<Char>

The file path from which to get the extension.

Returns

The extension of the specified path (including the period, "."), or Empty if path does not have extension information.

Remarks

This method obtains the extension of path by searching path for a period ("."), starting from the last character in the read-only span and continuing toward its first character. If a period is found before a DirectorySeparatorChar or AltDirectorySeparatorChar character, the returned read-only span contains the period and the characters after it; otherwise, ReadOnlySpan<T>.Empty is returned.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

GetExtension(String)

Source:
Path.cs
Source:
Path.cs
Source:
Path.cs

Returns the extension (including the period ".") of the specified path string.

C#
public static string GetExtension(string path);
C#
public static string? GetExtension(string? path);

Parameters

path
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

.NET Framework and .NET Core versions older than 2.1: path contains one or more of the invalid characters defined in GetInvalidPathChars().

Examples

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

C#
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 ''

Remarks

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

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0