logo VB.Net - Split a Filename
Guide Contents
DigitalDan Home Page

VB.met has a set of funtions to read a String variable that contains a fill filename (drive \ directory \ filename . extension) and split out the various parts. They are held in the IO.Path namespace.
The program comment below each line indicates the expected results.
 
Dim s As String
s = IO.Path.GetDirectoryName("c:\path\myfile.txt")
'   "c:\mypath"
s = IO.Path.GetFileNameWithoutExtension("c:\path\myfile.txt")
'   "myfile"
s = IO.Path.GetExtension("c:\path\myfile.txt")
'   ".txt"
s = IO.Path.GetFullPath("c:\path\myfile.txt")
'   "c:\mypath\myfile.txt"
s = IO.Path.GetPathRoot("c:\path\myfile.txt")
'   "c:\"
s = IO.Path.HasExtension("c:\path\myfile.txt").ToString
'   True
The IO.Path.Get... are very reliable but the IO.Path.GetDirectoryName has an unuual quirk
Although this normally return the path withouth the final "\" it will include the "\" when the file is in the root directory.
IO.Path.GetDirectoryName("c:\myfile.txt") returns "c:\"
IO.Path.GetDirectoryName("c:\mypath\myfile.txt") returns "c:\mypath"

DigitalDan.co.uk ... Hits = 143