A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to get extension of a given file
Q:

C# program to get extension of a given file

0

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now