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
Path methods in C#
To handle File Paths in C#, use the Path methods. These methods come under the System.IO namespace and provide a reliable way to work with file and directory paths across different operating systems.
The Path class contains static methods that perform common operations on strings that contain file or directory path information. These methods handle path separators, invalid characters, and other platform-specific details automatically.
Syntax
Following is the syntax for commonly used Path methods −
string extension = Path.GetExtension(path); string fileName = Path.GetFileName(path); string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path); string directoryName = Path.GetDirectoryName(path); string fullPath = Path.GetFullPath(path);
Path.GetExtension
The GetExtension() method retrieves the extension of a file from its path, including the dot (.) character −
using System;
using System.IO;
class Program {
static void Main() {
string filePath = @"C:\Documents\report.pdf";
string extension = Path.GetExtension(filePath);
Console.WriteLine("File Extension: " + extension);
string noExtFile = @"C:\Documents\readme";
string noExt = Path.GetExtension(noExtFile);
Console.WriteLine("No Extension: '" + noExt + "'");
}
}
The output of the above code is −
File Extension: .pdf No Extension: ''
Path.GetFileName
The GetFileName() method extracts the file name and extension from a full path −
using System;
using System.IO;
class Program {
static void Main() {
string fullPath = @"C:\Users\Documents\data.xml";
string fileName = Path.GetFileName(fullPath);
Console.WriteLine("File Name: " + fileName);
string directoryPath = @"C:\Users\Documents";
string dirResult = Path.GetFileName(directoryPath);
Console.WriteLine("Directory Result: '" + dirResult + "'");
}
}
The output of the above code is −
File Name: data.xml Directory Result: ''
Path.GetFileNameWithoutExtension
The GetFileNameWithoutExtension() method returns only the file name without the extension −
using System;
using System.IO;
class Program {
static void Main() {
string filePath = @"C:\Projects\application.exe";
string nameOnly = Path.GetFileNameWithoutExtension(filePath);
Console.WriteLine("Name without extension: " + nameOnly);
string multipleDotsFile = @"C:\Backup\archive.tar.gz";
string multiResult = Path.GetFileNameWithoutExtension(multipleDotsFile);
Console.WriteLine("Multiple dots result: " + multiResult);
}
}
The output of the above code is −
Name without extension: application Multiple dots result: archive.tar
Additional Path Methods
Here are other useful Path methods for comprehensive path manipulation −
using System;
using System.IO;
class Program {
static void Main() {
string filePath = @"C:\Users\Documents\project\file.txt";
Console.WriteLine("GetDirectoryName: " + Path.GetDirectoryName(filePath));
Console.WriteLine("GetFullPath: " + Path.GetFullPath("file.txt"));
Console.WriteLine("GetPathRoot: " + Path.GetPathRoot(filePath));
Console.WriteLine("HasExtension: " + Path.HasExtension(filePath));
string combined = Path.Combine(@"C:\Users", "Documents", "newfile.txt");
Console.WriteLine("Combined Path: " + combined);
}
}
The output of the above code is −
GetDirectoryName: C:\Users\Documents\project GetFullPath: C:\Program Files\dotnet\file.txt GetPathRoot: C:\ HasExtension: True Combined Path: C:\Users\Documents\newfile.txt
Comparison of Path Methods
| Method | Purpose | Example Input | Example Output |
|---|---|---|---|
| GetExtension | Gets file extension | @"C:\file.pdf" | |
| GetFileName | Gets filename with extension | @"C:\docs\file.pdf" | file.pdf |
| GetFileNameWithoutExtension | Gets filename only | @"C:\docs\file.pdf" | file |
| GetDirectoryName | Gets directory path | @"C:\docs\file.pdf" | C:\docs |
Conclusion
The Path class in C# provides essential methods for manipulating file and directory paths safely across different platforms. These methods handle path separators and invalid characters automatically, making path operations more reliable than manual string manipulation.
