Home » C# programs » C# DirectoryInfo class programs

C# program to get extension of a given file

In this article, we will learn how to get extension of file using Extension property? Here we will use class DirectoryInfo.
Submitted by IncludeHelp, on November 16, 2017

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

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);
        }
    }
}

Output

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.