C# Path GetExtension

Description

Path GetExtension Returns the extension of the specified path string.

Syntax

Path.GetExtension has the following syntax.


public static string GetExtension(
  string path
)

Parameters

Path.GetExtension has the following parameters.

  • path - The path string from which to get the extension.

Returns

Path.GetExtension method returns The extension of the specified path (including the period "."), or null, or String.Empty. If path is null, GetExtension returns null. If path does not have extension information, GetExtension returns String.Empty.

Example

The following code example demonstrates using the GetExtension method on a Windows-based desktop platform.


/*from   w ww  .  j  a v a 2 s .  c om*/
using System;
using System.IO;
public class MainClass
{
    public static void Main(String[] argv)
    {
        string fileName = @"C:\mydir.old\myfile.ext";
        string extension;

        extension = Path.GetExtension(fileName);
        Console.WriteLine(extension);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter