Home
VB.NET
Path.GetExtension: File Extension
Updated Jun 12, 2022
Dot Net Perls
File extension. A file has an optional extension—it occurs at the end of the file name. With the VB.NET Path.GetExtension Function we acquire the extension.
Function notes. We then make decisions based on the result String of Path.GetExtension. This function has some interesting behavior.
File
In this example, you can see that the string literal contains a Windows file system path. Next, the extension is extracted by invoking the Path.GetExtension method.
Finally The program shows exactly what the extension is. The period is included at the start of the three-character extension.
Imports System.IO Module Module1 Sub Main() ' Input path. Dim p As String = "C:\Users\Sam\Documents\Test.txt" ' Get extension. Dim extension As String = Path.GetExtension(p) ' Display extension. Console.WriteLine(extension) Console.WriteLine(".txt" = extension) End Sub End Module
.txt True
When using Path.GetExtension, developers may assume the period is not included in the extension string. We can use a string literal (like ".txt") to compare.
Path
Summary. With Path.GetExtension we reliably determine what the extension of a file name or path is. Many inputs are accepted by this function. It allows for many different formats of paths.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 12, 2022 (rewrite).
Home
Changes
© 2007-2025 Sam Allen