Path.HasExtension 方法

定义

重载

HasExtension(ReadOnlySpan<Char>)

确定指定字符范围表示的路径是否包含文件扩展名。

HasExtension(String)

确定路径是否包括文件扩展名。

HasExtension(ReadOnlySpan<Char>)

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

确定指定字符范围表示的路径是否包含文件扩展名。

public:
 static bool HasExtension(ReadOnlySpan<char> path);
public static bool HasExtension (ReadOnlySpan<char> path);
static member HasExtension : ReadOnlySpan<char> -> bool
Public Shared Function HasExtension (path As ReadOnlySpan(Of Char)) As Boolean

参数

path
ReadOnlySpan<Char>

用于搜索扩展名的路径。

返回

如果路径中最后一个目录分隔符或卷分隔符之后的字符包括句点 (.),并且后面跟有一个或多个字符,则为 true;否则为 false

注解

中的 path 尾随句点不被视为扩展。

另请参阅

适用于

HasExtension(String)

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

确定路径是否包括文件扩展名。

public:
 static bool HasExtension(System::String ^ path);
public static bool HasExtension (string path);
public static bool HasExtension (string? path);
static member HasExtension : string -> bool
Public Shared Function HasExtension (path As String) As Boolean

参数

path
String

用于搜索扩展名的路径。

返回

如果路径中最后一个目录分隔符(\ 或 /)或卷分隔符 (:) 之后的字符包括句点 (.),并且后面跟有一个或多个字符,则为 true;否则为 false

例外

.NET Framework 和 2.1 之前的 .NET Core 版本:path包含 中GetInvalidPathChars()定义的一个或多个无效字符。

示例

下面的示例演示 HasExtension 方法的用法。

String^ fileName1 = "myfile.ext";
String^ fileName2 = "mydir\\myfile";
String^ path = "C:\\mydir.ext\\";
bool result;
result = Path::HasExtension( fileName1 );
Console::WriteLine( "HasExtension('{0}') returns {1}", fileName1, result.ToString() );
result = Path::HasExtension( fileName2 );
Console::WriteLine( "HasExtension('{0}') returns {1}", fileName2, result.ToString() );
result = Path::HasExtension( path );
Console::WriteLine( "HasExtension('{0}') returns {1}", path, result.ToString() );

// This code produces output similar to the following:
//
// HasExtension('myfile.ext') returns True
// HasExtension('mydir\myfile') returns False
// HasExtension('C:\mydir.ext\') returns False
string fileName1 = "myfile.ext";
string fileName2 = @"mydir\myfile";
string path = @"C:\mydir.ext\";
bool result;

result = Path.HasExtension(fileName1);
Console.WriteLine("HasExtension('{0}') returns {1}",
    fileName1, result);

result = Path.HasExtension(fileName2);
Console.WriteLine("HasExtension('{0}') returns {1}",
    fileName2, result);

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

// This code produces output similar to the following:
//
// HasExtension('myfile.ext') returns True
// HasExtension('mydir\myfile') returns False
// HasExtension('C:\mydir.ext\') returns False
Dim fileName1 As String = "myfile.ext"
Dim fileName2 As String = "mydir\myfile"
Dim pathname As String = "C:\mydir.ext\"
Dim result As Boolean

result = Path.HasExtension(fileName1)
Console.WriteLine("HasExtension('{0}') returns {1}", fileName1, result)

result = Path.HasExtension(fileName2)
Console.WriteLine("HasExtension('{0}') returns {1}", fileName2, result)

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

' This code produces output similar to the following:
'
' HasExtension('myfile.ext') returns True
' HasExtension('mydir\myfile') returns False
' HasExtension('C:\mydir.ext\') returns False

注解

从 的末尾 path开始,此方法搜索句点 (.) 后跟至少一个字符。 如果在遇到 、 或 字符之前DirectorySeparatorChar找到此模式,则此方法返回 trueVolumeSeparatorCharAltDirectorySeparatorChar

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

另请参阅

适用于