Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to get the extension of a file in C#
The Path.GetExtension() method in C# is used to extract the file extension from a file path. This method is part of the System.IO namespace and returns the extension portion of the path string, including the period (.).
Syntax
Following is the syntax for using Path.GetExtension() method −
public static string GetExtension(string path)
Parameters
- path: A string containing the file path from which to get the extension.
Return Value
The method returns a string containing the extension of the specified path (including the period), or an empty string if the path has no extension.
Using Path.GetExtension() with File Paths
Example
Here is a basic example showing how to get file extensions −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string myPath = "D:\new\quiz.txt";
string extension = Path.GetExtension(myPath);
Console.WriteLine("File path: " + myPath);
Console.WriteLine("Extension: " + extension);
}
}
The output of the above code is −
File path: D:\new\quiz.txt Extension: .txt
Using Path.GetExtension() with Different File Types
Example
This example demonstrates getting extensions from various file types −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string[] filePaths = {
"document.pdf",
"image.jpg",
"data.csv",
"program.cs",
"fileWithoutExtension",
"archive.tar.gz"
};
foreach (string path in filePaths) {
string extension = Path.GetExtension(path);
Console.WriteLine($"File: {path} | Extension: '{extension}'");
}
}
}
The output of the above code is −
File: document.pdf | Extension: '.pdf' File: image.jpg | Extension: '.jpg' File: data.csv | Extension: '.csv' File: program.cs | Extension: '.cs' File: fileWithoutExtension | Extension: '' File: archive.tar.gz | Extension: '.gz'
Handling Edge Cases
Example
This example shows how the method handles various edge cases −
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string[] testCases = {
"C:\folder\file.txt",
"file.txt",
".hiddenfile",
"folder",
"",
null
};
foreach (string testCase in testCases) {
try {
string extension = Path.GetExtension(testCase);
Console.WriteLine($"Path: '{testCase}' | Extension: '{extension}'");
}
catch (Exception ex) {
Console.WriteLine($"Path: '{testCase}' | Error: {ex.GetType().Name}");
}
}
}
}
The output of the above code is −
Path: 'C:\folder\file.txt' | Extension: '.txt' Path: 'file.txt' | Extension: '.txt' Path: '.hiddenfile' | Extension: '' Path: 'folder' | Extension: '' Path: '' | Extension: '' Path: '' | Error: ArgumentNullException
Common Use Cases
| Use Case | Description |
|---|---|
| File Type Validation | Check if uploaded files have allowed extensions |
| File Processing | Route files to appropriate handlers based on extension |
| File Organization | Group or sort files by their type/extension |
Conclusion
The Path.GetExtension() method provides a reliable way to extract file extensions from paths in C#. It handles various edge cases and returns the extension including the period, making it essential for file type validation and processing operations.
