Export (0) Print
Expand All

Path.HasExtension Method

Determines whether a path includes a file name extension.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function HasExtension ( _
	path As String _
) As Boolean

Parameters

path
Type: System.String

The path to search for an extension.

Return Value

Type: System.Boolean
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.

ExceptionCondition
ArgumentException

path contains one or more of the invalid characters defined in GetInvalidPathChars.

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.

The following code example demonstrates the use of the HasExtension method.

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft