Path: GetExtension

GetExtension

Returns the extension of the specified path string.



 Public Function GetExtension(
	  ByRef Path As String ) As String

Parameters

Path
[ByRef] String. The path string from which to get the extension.

Return Values

String -  The extension of the specified path (including the period "."), or an empty string. If Path is empty, GetExtension returns an empty string. If Path does not have extension information, GetExtension returns an empty string.

Remarks

The extension of Path is obtained by searching path for a period (.), starting with the last character in Path and continuing toward the start of Path. If a period is found before a DirectorySeparatorChar or AltDirectorySeparatorChar character, the returned string contains the period and the characters after it; otherwise, empty string is returned.

Exceptions

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

Examples

The following code example demonstrates using the GetExtension method.

Public Sub Main()
    Const FileName As String = "C:\MyDir.Old\MyFile.Ext"
    Const PathName As String = "C:\MyDir.Old\"
    
    Dim Extension As String
    
    Extension = Path.GetExtension(FileName)
    Debug.Print CorString.Format("GetExtension('{0}') return '{1}'", FileName, Extension)
    
    Extension = Path.GetExtension(PathName)
    Debug.Print CorString.Format("GetExtension('{0}') return '{1}'", PathName, Extension)
End Sub

' This example code produces the following output.
'
'    GetExtension('C:\MyDir.Old\MyFile.Ext') returns '.Ext'
'    GetExtension('C:\MyDir.Old\') returns ''

See Also

Project CorLib Overview

Class Path Overview