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.
Example. 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:\
Example output. 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.
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:\
Combine path strings. 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.
Tip By using the 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
Separator characters. Several 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.
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/
\
;
:
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.
Tip Successive calls to the GetTempFileName function will yield different temporary file names.
Info The 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 names. How 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.
Tip Each successive call to this function will result in a different randomized file name. The directory is not included.
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
Extensions. 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".
Note In code that acts upon extensions, you must include the leading dot in comparisons.
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 21, 2024 (edit link).