Path: GetFileName

GetFileName

Returns the file name and extension of the specified path string.



 Public Function GetFileName(
	  ByRef Path As String ) As String

Parameters

Path
[ByRef] String. The path string from which to obtain the file name and extension.

Return Values

String -  The characters after the last directory character in Path. If the last character of Path is a directory or volume separator character, this method returns an empty string. If Path is empty, this method returns an empty string.

Remarks

The returned value is an empty string if the file path is an empty string.

The separator characters used to determine the start of the file name are DirectorySeparatorChar and AltDirectorySeparatorChar.

Exceptions

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

Examples

The following code example demonstrates the behavior of the GetFileName method.

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

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

See Also

Project CorLib Overview

Class Path Overview