Path: HasExtension

HasExtension

Determines whether a path includes a file name extension.



 Public Function HasExtension(
	  ByRef Path As String ) As Boolean

Parameters

Path
[ByRef] String. The path to search for an extension.

Return Values

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.

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.

Exceptions

ExceptionCondition
ArgumentException Path contains one or more of the invalid characters defined in GetInvalidPathChars.

Examples

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

Public Sub Main()
    Const FileName1 As String = "MyFile.Ext"
    Const FileName2 As String = "MyDir\MyFile"
    Const AbsolutePath As String = "C:\MyDir.Ext\"
    
    Dim Result As Boolean
    
    Result = Path.HasExtension(FileName1)
    Debug.Print CorString.Format("HasExtension('{0}') returns {1}", FileName1, Result)
    
    Result = Path.HasExtension(FileName2)
    Debug.Print CorString.Format("HasExtension('{0}') returns {1}", FileName2, Result)
    
    Result = Path.HasExtension(AbsolutePath)
    Debug.Print CorString.Format("HasExtension('{0}') returns {1}", AbsolutePath, Result)
End Sub

' This example code produces the following output.
'
'    HasExtension('MyFile.Ext') returns True
'    HasExtension('MyDir\MyFile') returns False
'    HasExtension('C:\MyDir.Ext\') returns False

See Also

Project CorLib Overview

Class Path Overview

DirectorySeparatorChar

AltDirectorySeparatorChar

VolumeSeparatorChar