Επεξεργασία

Κοινή χρήση μέσω


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.

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

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

GetExtension(String)

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

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

public:
 static System::String ^ GetExtension(System::String ^ path);
public static string GetExtension (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

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.

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

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