Given a file and we have to get its extension using C# program.
DirectoryInfo.Extension
This is a method of "DirectoryInfo" class, it returns the extension of a given file.
Syntax:
string DirectoryInfo.Extension
Return value:
Returns the extension of given file.
Program
Output
It this program we used Extension property of DirectoryInfo, get extension of file "sample.txt", that is ".txt".
Note: In above program, we need to remember, when we use "DirectoryInfo" class, System.IO namespace must be included in the program.