Using IO.Path.GetDirectoryName(filename) and IO.Path.GetFileNameWithoutExtension(filename)
Private Sub PathDemo1()
Dim mydocs, filename, directoryname, namewithoutextension, sdx As String
' find the documents folder
mydocs = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
'create a known file name to demo io.path
filename = mydocs + "\" + "TheLonelyProgrammer.txt"
Dim sw As New StreamWriter(filename)
sw.WriteLine("This is a line of text")
sw.Close()
sdx = String.Format("Present file name {0}", filename)
Console.WriteLine(sdx)
Console.WriteLine()
' Now that we have a known file here is how IO.Path is used
directoryname = IO.Path.GetDirectoryName(filename)
sdx = String.Format("Results of Path.GetDirectoryName(filename) {0}", filename)
Console.WriteLine(sdx)
Console.WriteLine()
namewithoutextension = IO.Path.GetFileNameWithoutExtension(filename)
sdx = String.Format("Results of Path.GetFileNameWithoutExtension {0}", namewithoutextension)
Console.WriteLine(sdx)
End Sub
Output:
Present file name C:\Users\John\Documents\TheLonelyProgrammer.txt
Results of Path.GetDirectoryName(filename) C:\Users\John\Documents
Results of Path.GetFileNameWithoutExtension TheLonelyProgrammer
The creation of the file was so that I knew the location of
a file without browsing for it. That was in order to
demonstrate the use IO.Path.GetDirectoryName(filename) and
IO.Path.GetFileNameWithoutExtension(filename). Notice the
GetFileNameWithoutExtension only returns the filename and
not the whole path. The object browser implies that the
entire path is return minus the extension.
Using IO.Path.ChangeExtension(filename, extension) & IO.Path.GetFullPath(newfilename)
Private Sub PathDemo2()
Dim mydocs, filename, newfilename As String
Dim fullname, pathroot, sdx As String
mydocs = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
'create a known file name to demo io.path
filename = mydocs + "\" + "TheLonelyProgrammer.txt"
Dim sw As New StreamWriter(filename)
sw.WriteLine("This is a line of text")
sw.Close()
sdx = String.Format("Present file name {0}", filename)
Console.WriteLine(sdx)
Console.WriteLine()
newfilename = IO.Path.ChangeExtension(filename, "log")
sdx = String.Format("New filename = {0} ", newfilename)
Console.WriteLine(sdx)
Console.WriteLine()
fullname = IO.Path.GetFullPath(newfilename)
sdx = String.Format("Results of GetFullPath= {0} ", fullname)
Console.WriteLine(sdx)
Console.WriteLine()
pathroot = IO.Path.GetPathRoot(newfilename)
sdx = String.Format("Results of GetPathRoot= {0} ", pathroot)
Console.WriteLine(sdx)
End Sub
Output:
Present file name C:\Users\John\Documents\TheLonelyProgrammer.txt
New filename = C:\Users\John\Documents\TheLonelyProgrammer.log
Results of GetFullPath= C:\Users\John\Documents\TheLonelyProgrammer.log
Results of GetPathRoot= C:\
IO.Path.ChangeExtension is a handy tool. I use it when I
want output files to have the same name as the input
file. IO.Path.GetFullPath returns the full path along with
the file name. If you want the location only use
IO.Path.GetDirectoryName
Using IO.Path.GetRandomFileName, IO.Path.GetTempFileName &
IO.Path.GetTempPath
Private Sub PathDemo3()
Dim randomfile, tempfile, temppath, sdx As String
randomfile = IO.Path.GetRandomFileName
sdx = String.Format("Random File Name = {0}", randomfile)
Console.WriteLine(sdx)
Console.WriteLine()
tempfile = IO.Path.GetTempFileName
sdx = String.Format("Temp File Name {0};", tempfile)
Console.WriteLine(sdx)
Console.WriteLine()
temppath = IO.Path.GetTempPath
sdx = String.Format("Temp Path {0};", TempPath)
Console.WriteLine(sdx)
End Sub
Output:
Random File Name = 5kto0uqx.kov
Temp File Name C:\Users\John\AppData\Local\Temp\tmpEC20.tmp;
Temp Path C:\Users\John\AppData\Local\Temp\;
GetRandomFileName, GetTempFileName, and GetTempPath
come are useful when one needs to write and read to
a file that is temporary and won't be needed indefinitely.
600
1200
1800
2400
3000
3600
4200
4800
5400
6000
6600
7200
7800
8400
9000
9600
10200
10800
11400
12000
12600
13200
13800
14400
15000
15600
16200
16800
17400
18000
18600
19200