C# - DirectoryInfo.Extension Property with Example

Learn, how to get extension of file using Extension property using C# program?
Submitted by IncludeHelp, on November 16, 2017 [Last updated : March 27, 2023]

Given a file and we have to get its extension using C# program.


The Extension is a property of "DirectoryInfo" class, it returns the extension of a given file.


string DirectoryInfo.Extension


  1. None

Return Value

Returns the extension of given file.

C# program to get extension of a given file

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      DirectoryInfo d = new DirectoryInfo("sample.txt");

      Console.WriteLine("File extension is : " + d.Extension);


File extension is : .txt


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.

C#.Net DirectoryInfo Class Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.