VB.NET Path Examples

Use the Path type to handle file locations in a consistent way.

dot net perls

Path. The Path type offers many useful Functions. They provide ways to manipulate strings that represent file paths. By using the Path type, we avoid writing a lot of string manipulation code over and over again.

Example. First we look at the result of the function Path.GetExtension. We also see GetFileName, GetFileNameWithoutExtension and GetPathRoot. Please notice how the Imports line at the top references the System.IO namespace.
VB.NET program that uses Path functions Imports System.IO Module Module1 Sub Main() ' Input string. Dim value As String = "C:\stagelist.txt" ' Use Path methods. Dim extension As String = Path.GetExtension(value) Dim filename As String = Path.GetFileName(value) Dim filenameNotExtension As String = Path.GetFileNameWithoutExtension(value) Dim root As String = Path.GetPathRoot(value) ' Print results. Console.WriteLine(extension) Console.WriteLine(filename) Console.WriteLine(filenameNotExtension) Console.WriteLine(root) End Sub End Module Output .txt stagelist.txt stagelist C:\

Example output. How well does the Path functions handle invalid input? Sometimes, you will pass a path string that does not have an extension, a directory, or a volume. The separator character may not be the default one as well.
Here: This program demonstrates the result of 3 Path functions on 6 input path strings.
VB.NET program that tests multiple path strings Imports System.IO Module Module1 Sub Main() Dim pages() As String = { _ "cat.aspx", _ "invalid-page", _ "Content/Rat.aspx", _ "http://dotnetperls.com/Cat/Mouse.aspx", "C:\Windows\File.txt", "C:\Word-2007.docx" _ } For Each page As String In pages ' Write path function results. Console.Write(Path.GetFileName(page)) Console.Write(", ") Console.Write(Path.GetFileNameWithoutExtension(page)) Console.Write(", ") Console.Write(Path.GetDirectoryName(page)) Console.WriteLine() Next End Sub End Module Output cat.aspx, cat, invalid-page, invalid-page, Rat.aspx, Rat, Content Mouse.aspx, Mouse, http:\dotnetperls.com\Cat File.txt, File, C:\Windows Word-2007.docx, Word-2007, C:\

Combine path strings. One common problem in VB.NET is finding a way to combine a directory string with a file name. Sometimes, the directory name may have a slash on it. Sometimes it may not. The Path.Combine function handles both of these cases.
Tip: By using the Path.Combine function, you can handle different inputs correctly, and the output will not have duplicate separators.
VB.NET program that uses Path.Combine function Imports System.IO Module Module1 Sub Main() ' Combine directory with file name. Dim value1 As String = Path.Combine("Content", "file.txt") Console.WriteLine(value1) ' Combine directory and trailing backslash with file name. Dim value2 As String = Path.Combine("Content\", "file.txt") Console.WriteLine(value2) End Sub End Module Output Content\file.txt Content\file.txt

Separator characters. On the Path type, there are several different char members that represent separators. For example, the DirectorySeparatorChar is equal to "\", while the AltDirectorySeparatorChar is equal to "/".
Next: In the example, we print the 4 separator characters to the output with Console.WriteLine.
VB.NET program that outputs separators Imports System.IO Module Module1 Sub Main() ' Write these chars to the screen. Console.WriteLine(Path.AltDirectorySeparatorChar) Console.WriteLine(Path.DirectorySeparatorChar) Console.WriteLine(Path.PathSeparator) Console.WriteLine(Path.VolumeSeparatorChar) End Sub End Module Output / \ ; :

Temporary file paths. In many programs, you may want to write to a temporary file location. This is a temporary location because it will not be needed later for any reason. To do this, please use the GetTempFileName function on the Path type.

Successive calls to the GetTempFileName function will yield different temporary file names. The GetTempPath function, on the other hand, yields the same path each time. It is important to choose the right function.
VB.NET program that writes temp file paths Imports System.IO Module Module1 Sub Main() ' Write temp file names and then temp paths. Console.WriteLine(Path.GetTempFileName()) Console.WriteLine(Path.GetTempFileName()) Console.WriteLine() Console.WriteLine(Path.GetTempPath()) Console.WriteLine(Path.GetTempPath()) End Sub End Module Output C:\Users\Sam\AppData\Local\Temp\tmpF664.tmp C:\Users\Sam\AppData\Local\Temp\tmpF665.tmp C:\Users\Sam\AppData\Local\Temp\ C:\Users\Sam\AppData\Local\Temp\

Random file names. How can you generate a random file name in the VB.NET language? You might want to do this to write to a secret location or to create a unique file that is not a temporary file. Please invoke the Path.GetRandomFileName function.
Tip: Each successive call to this function will result in a different randomized file name. The directory is not included.
VB.NET program that writes random file names Imports System.IO Module Module1 Sub Main() ' Output two random file names. Console.WriteLine(Path.GetRandomFileName()) Console.WriteLine(Path.GetRandomFileName()) End Sub End Module Output d5mgpsqn.gce mseds0ay.1am

Extensions. When you call the Path.GetExtension method in the VB.NET language, your extension will contain the leading period. In other words, ".txt" is the extension for a text file, not "txt".
Note: In code that acts upon extensions, you must include the leading dot in comparisons.

Summary. The Path functions in the VB.NET language are exceedingly useful and provide a useful level of error-correction in your programs. By using these methods, your programs will become more robust and more compact.


© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.