Edit

Share via


Path.HasExtension Method

Definition

Overloads

HasExtension(ReadOnlySpan<Char>)

Determines whether the path represented by the specified character span includes a file name extension.

HasExtension(String)

Determines whether a path includes a file name extension.

HasExtension(ReadOnlySpan<Char>)

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

Determines whether the path represented by the specified character span includes a file name extension.

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

Parameters

path
ReadOnlySpan<Char>

The path to search for an extension.

Returns

true if the characters that follow the last directory separator character or volume separator in the path include a period (".") followed by one or more characters; otherwise, false.

Remarks

A trailing period in path is not considered an extension.

See also

Applies to

HasExtension(String)

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

Determines whether a path includes a file name extension.

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

Parameters

path
String

The path to search for an extension.

Returns

true if the characters that follow the last directory separator (\ or /) or volume separator (:) in the path include a period (.) followed by one or more characters; otherwise, false.

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 the use of the HasExtension method.

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

Remarks

Starting from the end of path, this method searches for a period (.) followed by at least one character. If this pattern is found before a DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar character is encountered, this method returns true.

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

See also

Applies to