Mega Code Archive

 
Categories / VB.Net Tutorial / Data Type
 

Use the LastIndexOf(Char) method to find the last directory separator character in a string

Imports System.IO Public Module Test    Public Sub Main()       Dim filename As String        filename = ExtractFilename("C:\delegate.txt")        Console.WriteLine(filename)    End Sub    Public Function ExtractFilename(filepath As String) As String       If filepath.Trim().EndsWith("\") Then Return String.Empty       Dim position As Integer = filepath.LastIndexOf("\"c)       If position = -1 Then          If File.Exists(Environment.CurrentDirectory + Path.DirectorySeparatorChar + filepath) Then             Return filepath          Else             Return String.Empty          End If       Else          If File.Exists(filepath) Then             Return filepath.Substring(position + 1)          Else             Return String.Empty          End If                            End If    End Function End Module