C# Program to Get Extension of a Given File Last Updated : 16 Feb, 2022 Comments Improve Suggest changes Like Article Like 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 Comment More infoAdvertise with us Next Article C# Program to Get Extension of a Given File sravankumar_171fa07058 Follow Improve Article Tags : C# C# Programs Similar Reads 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 t 2 min read C# Path Class - Basics Operations C# path class comes under System.IO namespace and System.Runtime.dll assembly. This class is used to perform operations on string instances that have file path or directory path information. A path is a string that holds the location of the file or directory and it can be an absolute or relative loc 3 min read How to get a File Extension in PHP ? In this article, we will learn how to get the current file extensions in PHP. Input : c:/xampp/htdocs/project/home Output : "" Input : c:/xampp/htdocs/project/index.php Output : ".php" Input : c:/xampp/htdocs/project/style.min.css Output : ".css" Using $_SERVER[âSCRIPT_NAMEâ]: $_SERVER is an array o 2 min read C++ Program to Get the List of Files in a Directory Getting the list of files in a directory is one of the most common operations performed by the Filesystem of an OS. The file explorer application in most operating systems performs the same operation in the background. In this article, you will learn how to get the list of files in a directory using 4 min read How to Get File Extension in C++? In C++, we may often find the need to extract the file extension from a given path of the file while working in many applications for processing or validating. In this article, we will learn how to get the file extension in C++. For Example, Input: someFolder â³ filename.ext Output: File Extension = 2 min read Get the File Extension from a URL in Python Handling URLs in Python often involves extracting valuable information, such as file extensions, from the URL strings. However, this task requires careful consideration to ensure the safety and accuracy of the extracted data. In this article, we will explore four approaches to safely get the file ex 2 min read How to Change File Extension in Mac? File Extensions or Filename Extensions are algorithm-specific elements that can be opened only with the specific application based on the algorithm mentioned there. There are a large number of file extensions present and can be different based on the Operating System. Like, the compressed file forma 3 min read C Program To Find Initials of a Name Here, we will see how to find the initials of a name using a C program. Below are the examples: Input: Geeks for GeeksOutput: G F GWe take the first letter of allwords and print in capital letter. Input: Jude LawOutput: J L Approach: Print the first character in the capital. Traverse the rest of the 2 min read Change File Extension In Python Changing file extensions in Python can be a common task when working with files. Whether you need to modify file types for compatibility, organize your files, or perform some other operation, Python provides several methods to achieve this. In this article, we will explore four different methods to 3 min read How to get filename without extension in Ruby? In this article, we will learn how to get a filename in Ruby without its extension. We can use the File.basename method to extract the last name of a filename. In Ruby, the File.basename method returns the last component of the filename. Syntax: File.basename(file_path [,suffix]) If we don't provide 1 min read Like