Path.GetExtension 方法

定义

重载

GetExtension(String)

返回指定路径字符串的扩展名(包括句点“.”)。Returns the extension (including the period ".") of the specified path string.

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.

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

参数

path
String

从中获取扩展名的路径字符串。The path string from which to get the extension.

返回

String

指定路径的扩展名(包含句点“.”)、或 null、或 EmptyThe extension of the specified path (including the period "."), or null, or Empty. 如果 pathnull,则 GetExtension(String) 返回 nullIf path is null, GetExtension(String) returns null. 如果 path 不具有扩展名信息,则 GetExtension(String) 返回 EmptyIf path does not have extension information, GetExtension(String) returns Empty.

例外

.NET Framework 和 .NET Core 版本早于2.1: path 包含中定义的一个或多个无效字符 GetInvalidPathChars().NET Framework and .NET Core versions older than 2.1: path contains one or more of the invalid characters defined in GetInvalidPathChars().

示例

下面的示例演示如何 GetExtension 在基于 Windows 的桌面平台上使用方法。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 ''

注解

此方法 path 通过搜索句点 ( 获取扩展 path 。 ) ,从中的最后一个字符开始,一直到 path 第一个字符。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. 如果在或字符之前找到句点 DirectorySeparatorChar AltDirectorySeparatorChar ,则返回的字符串将包含句点和后面的字符; 否则 String.Empty 返回。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.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于

GetExtension(ReadOnlySpan<Char>)

返回由只读字符范围表示的文件路径的扩展名。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)

参数

path
ReadOnlySpan<Char>

从中获取扩展名的文件路径。The file path from which to get the extension.

返回

ReadOnlySpan<Char>

指定路径的扩展名(包括句点“.”);如果 path 没有扩展信息,则为 EmptyThe extension of the specified path (including the period, "."), or Empty if path does not have extension information.

注解

此方法 path 通过搜索 path 句点 ( "。) ,从只读范围中的最后一个字符开始直到其第一个字符。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. 如果在或字符之前找到句点 DirectorySeparatorChar AltDirectorySeparatorChar ,则返回的只读范围将包含句点和后面的字符; 否则 ReadOnlySpan<T>.Empty 返回。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.

适用于