Read the file name from String

georgepatotk

Well-known member
Joined
Mar 1, 2004
Messages
431
Location
Malaysia
I have the a file with the full path as shown below:
c:/the directory/pic.jpg

any function for me to use as to pick up only the file name?
the result I want from the above mentioned path is "pic.jpg"
 
I found the solution

Dim theName as string
theName = "c:/the directory/pic.jpg"
theName = theName.Substring(theName.LastIndexOf("\") + 1)
 
dim strPath as string = "c:\directory\pic.jpg"
dim strFile as string
dim intPos as integer

intPos = strPath.lastindexof("\")

strFile = strPath.Substring(intPos + 1)
 
The GetFileName method of the IO.Path class will do what you want. It is way more elegant then trying to extract it yourself.
Code:
Dim filename As String = System.IO.Path.GetFileName("path")
 
Back
Top