Open In App

C# Program to Get Extension of a Given File

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

DirectoryInfo class provides different types of methods and properties that are used to perform operations on directories and sub-directories like creating, moving, etc. This class has an Extension property which is used to find the extension part from the given file name which includes the dot format in the file’s full name. For example, if the file name is c:\gfg.txt, then this property will return “.txt”.

Syntax:

public string Extension { get; }

Return: It will return a string with an extension in the dot format of the current file. Even if it is the full file name or an empty string, or if no extension is available. 

Example:

C#




// C# program to find the extension of a given File
using System;
using System.IO;
 
class GFG{
 
static void Main()
{
 
    // Specify text file
    DirectoryInfo extension = new DirectoryInfo("my_data.txt");
 
    // Get the extension of the File
    // Using Extension property
    Console.WriteLine("File extension : " + extension.Extension);
 
    // Specify pdf file
    DirectoryInfo extension1 = new DirectoryInfo("my_data.pdf");
 
    // Get the extension of the File
    // Using Extension property
    Console.WriteLine("File extension : " + extension1.Extension);
 
    // Specify the file which has no extension
    DirectoryInfo extension2 = new DirectoryInfo("gfg");
 
    // Get the extension of the File
    // Using Extension property
    Console.WriteLine("File extension : " + extension2.Extension);
 
    // Specify the file which has multiple dots
    DirectoryInfo extension3 = new DirectoryInfo("gfg.gg.txt");
 
    // Get the extension of the File
    // Using Extension property
    Console.WriteLine("File extension : " + extension3.Extension);
}
}


Output:

File extension : .txt
File extension : .pdf
File extension : 
File extension : .txt

Similar Reads

C# Program To Copy Content Of One File To Another File By Overwriting Same File Name
Given a file, now our task is to copy data from one file to another file by overwriting the same file name using C#. So we use the following methods to perform this task: 1. Copy(String, String, Boolean): It is used to copy the content of one file to a new file with overwrite. Syntax: File.Copy(Myfile1, Myfile2, owrite); Where Myfile1 is the first
2 min read
Check if a path has a file name extension in C#
Path.HasExtension Method is used to check whether the specified path has a file name extension or not. This method will start the searching for a period (.) followed by at least one character from the end of the path. If this pattern is found before a DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar character is encountered
2 min read
C# Program to Get File Time Using File Class
Given a file, now our task is to get the file time using the File class. So we use the GetCreationTime() method of the File class. This method is used to find the creation date and time of the given file or directory. This method will only take one parameter that is the path of the file and if this path parameter does not exist, then it will return
2 min read
C# Program to Count the Files Based on Extension using LINQ
Given files, now we count the files based on extension using LINQ. We are considering all types of file formats like pdf, txt, xml and going to count these files based on the extension. For that, we have to know the following methods: Path.GetExtension(): This method is used to get an extension of the given path.TrimStart(): This method is used to
2 min read
Extension Method in C#
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is introduced in C# 3.0. Let us discuss this concept
5 min read
C# Program to Get the Memory Size For OS Page File using Environment Class
Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environment variable settings information, contents of the
2 min read
C# - Reading Lines From a File Until the End of File is Reached
Given a file, now our task is to read lines from the file until the end of the file using C#. Before reading the lines from a file we must have some data, so first we need an empty file in our path and then insert lines in our file and then we read lines from a file. So to do this task we use two basic operations that are reading and writing. The f
2 min read
C# - Copying the Contents From One File to Another File
Given a file, now our task is to copy data from one file to another file using C#. So to do this task we use the Copy() method of the File class from the System.IO namespace. This function is used to copy content from one file to a new file. It has two different types of overloaded methods: 1. Copy(String, String): This function is used to copy con
3 min read
C# Program to Get Root Directory of Given Directory
Directory class provides different types of methods for creating, moving, deleting, renaming, and modifying directories and subdirectories. GetDirectoryRoot() is a method of Directory class. This method is used to find the information of the volume or root or both for the given path. Or we can say that this method is used to find the root directory
2 min read
C# Program to Get Computer Drive Names of Given Directory
Directory class provides different types of methods for creating, moving, deleting, renaming, and modifying directories and subdirectories. GetLogicalDrives() is the method of the Directory class. This method is used to find the name of the logical drive names present in the computer. Or we can say that this method returns the drive names of the gi
2 min read
Article Tags :