C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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:\
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://dotnetCodex.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:\dotnetCodex.com\Cat
File.txt, File, C:\Windows
Word-2007.docx, Word-2007, C:\
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
Next: In the example, we print the 4 separator characters to the output with Console.WriteLine.
ConsoleVB.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
/
\
;
:
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\
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
Note: In code that acts upon extensions, you must include the leading dot in comparisons.