This documentation is archived and is not being maintained.

Path.GetExtension Method

Returns the extension of the specified path string.

[Visual Basic]
Public Shared Function GetExtension( _
   ByVal path As String _
) As String
public static string GetExtension(
 string path
public: static String* GetExtension(
 String* path
public static function GetExtension(
   path : String
) : String;


The path string from which to get the extension.

Return Value

A String containing the extension of the specified path (including the "."), a null reference (Nothing in Visual Basic), or Empty. If path is a null reference (Nothing), GetExtension returns a null reference (Nothing). If path does not have extension information, GetExtension returns Empty.


Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in InvalidPathChars.


The extension of path is obtained by searching path for a period (.), starting with the last character in path and continuing toward the start of path. If a period is found before a DirectorySeparatorChar or AltDirectorySeparatorChar character, the returned string contains the period and the characters after it; otherwise, Empty is returned.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Retrieve the fully qualified path of a file. GetFullPath
Retrieve the file name and extension from a path. GetFileName
Retrieve only the file name from a path. GetFileNameWithoutExtension
Change the extension of a file. ChangeExtension


[Visual Basic, C#, C++] The following example demonstrates using the GetExtension method on a Windows-based desktop platform.

[Visual Basic] 
Dim fileName As String = "C:\mydir.old\myfile.ext"
Dim pathname As String = "C:\mydir.old\"
Dim extension As String

extension = Path.GetExtension(fileName)
Console.WriteLine("GetExtension('{0}') returns '{1}'", fileName, extension)

extension = Path.GetExtension(pathname)
Console.WriteLine("GetExtension('{0}') returns '{1}'", pathname, extension)

string fileName = @"C:\mydir.old\myfile.ext";
string path = @"C:\mydir.old\";
string extension;

extension = Path.GetExtension(fileName);
Console.WriteLine("GetExtension('{0}') returns '{1}'", 
    fileName, extension);

extension = Path.GetExtension(path);
Console.WriteLine("GetExtension('{0}') returns '{1}'", 
    path, extension);

String * fileName = S"C:\\mydir.old\\myfile.ext";
String * path = S"C:\\mydir.old\\";
String * extension;

extension = Path::GetExtension(fileName);
Console::WriteLine(S"GetExtension('{0}') returns '{1}'", 
   fileName, extension);

extension = Path::GetExtension(path);
Console::WriteLine(S"GetExtension('{0}') returns '{1}'", 
   path, extension);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Path Class | Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File