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.

public:
 property System::String ^ Extension { System::String ^ get(); };
public string Extension { get; }
member this.Extension : string
Public ReadOnly Property Extension As String

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

See also