Path
In VB.NET the Path
type offers many useful Functions. They provide ways to manipulate strings that represent file paths. With Path
, our main program logic can be kept simpler.
By using the Path
type, we avoid writing a lot of string
manipulation code over and over again. We can safely combine paths, and get parts of paths. This makes programs simpler.
First we look at the result of the function Path.GetExtension
. We also see GetFileName
, GetFileNameWithoutExtension
and GetPathRoot
. The Imports line references the System.IO
namespace.
Imports System.IO Module Module1 Sub Main() ' Input string. Dim value As String = "C:\stage.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.txt stage.txt stage C:\
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.
Path
functions on 6 input 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 Modulecat.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:\
With Path
we can combine a directory string
with a file name, with or without a trailing slash. The Path.Combine
function handles both cases.
Path.Combine
function, you can handle different inputs correctly, and the output will not have duplicate separators.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 ModuleContent\file.txt Content\file.txt
Several char
members that represent separators. For example, the DirectorySeparatorChar
is equal to "\", while the AltDirectorySeparatorChar
is equal to "/".
Console.WriteLine
.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/ \ ; :
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.
GetTempFileName
function will yield different temporary file names.GetTempPath
function, on the other hand, yields the same path each time. It is important to choose the right function.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 ModuleC:\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 namesHow can you generate a random file name? You might want to write to a secret location or create a unique file. Please invoke the Path.GetRandomFileName
function.
Imports System.IO Module Module1 Sub Main() ' Output two random file names. Console.WriteLine(Path.GetRandomFileName()) Console.WriteLine(Path.GetRandomFileName()) End Sub End Moduled5mgpsqn.gce mseds0ay.1am
When you call the Path.GetExtension
method, your extension will contain the leading period. In other words, ".txt" is the extension for a text file, not "txt".
The Path
functions are useful and provide error-correction in your programs. By using these methods, your programs will become more robust and more compact.