FileSystemInfo.Extension Property

Definition

Gets the extension part of the file name, including the leading dot . even if it is the entire file name, or an empty string if no extension is present.

C#
public string Extension { get; }

Property Value

A string containing the FileSystemInfo extension.

Remarks

The Extension property returns the FileSystemInfo extension, which starts at, and includes, the last dot (.) in the file's full name. For example:

  • For a file c:\NewFile.txt, this property returns ".txt".
  • For a file c:\.gitignore, this property returns ".gitignore".
  • For a file c:\noextension, this property returns an empty string "".
  • For a file c:\a.b.c, this property returns ".c".
  • When the FileInfo is constructed from a file path with a trailing dot, then that trailing dot is trimmed, so new FileInfo("foo.bar.").Extension returns ".bar".

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. How to: Write Text to a File
Write to a text file. How to: Write Text to a File
Read from a text file. How to: Read Text from a File
Retrieve the fully qualified path of a file. GetFullPath
Retrieve only the file name a path. GetFileNameWithoutExtension
Retrieve only the directory name from a path. GetDirectoryName
Change the extension of a file. ChangeExtension

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also