VB.NET File Extension

Path type

A file has an extension. This is optional. It occurs at the end of the file name. With the Path.GetExtension Function we acquire the extension. We then make decisions based on its value. This method has some interesting behavior.

Path

Example

Note

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. You pass the string reference to this function as an argument.

Finally:The program shows exactly what the extension is. The period is included at the start of the three-character extension.

Program that uses Path.GetExtension: VB.NET

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

Output

.txt
True
Programming tip

When using Path.GetExtension, developers may assume the period is not included in the extension string. To compare the extension string, you must use a string literal such as ".txt" with the period prefixing the three letters.

String Type, Functions

Summary

The VB.NET programming language

With the Path.GetExtension function in the VB.NET language, you can quickly and 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.


VB.NET: File