Summary: in this tutorial, you’ll learn how to use the C# Path
class to interact with the file and directory paths.
Introduction to the C# Path class
The Path
class is a part of the System.IO
namespace in .NET. The Path
class provides you with a set of useful static methods for working with the file and directory Path
.
Getting the file name
To get the file name from a path, you use the GetFileName()
method:
using static System.Console;
string path = @"C:\temp\readme.txt";
string fileName = Path.GetFileName(path);
WriteLine(fileName);
Code language: C# (cs)
Output:
readme.txt
Code language: C# (cs)
Getting the directory name
To get the directory name of the path, you use the GetDirectoryName()
method :
using static System.Console;
string path = @"C:\temp\readme.txt";
string dirname = Path.GetDirectoryName(path);
WriteLine(dirname);
Code language: C# (cs)
Output:
C:\temp
Code language: C# (cs)
Getting the absolute path
To get the absolute path of a specified path, you use the GetFullPath()
method:
using static System.Console;
string relativePath = @"..\..\readme.txt";
string fullPath = Path.GetFullPath(relativePath);
WriteLine(fullPath);
Code language: C# (cs)
Output:
C:\project\bin\readme.txt
Code language: C# (cs)
Combining paths
To combines two or more strings into a path, you use the Combine()
method:
using static System.Console;
string directory = @"C:\temp";
string fileName = "readme.txt";
string fullPath = Path.Combine(directory, fileName);
WriteLine(fullPath);
Code language: C# (cs)
Output:
C:\temp\readme.txt
Code language: C# (cs)
Getting file extension
To get the file extension of a path, you use the GetExtension()
method:
using static System.Console;
string path = @"C:\temp\readme.txt";
string extension = Path.GetExtension(path);
WriteLine(extension);
Code language: C# (cs)
Output:
.txt
Code language: CSS (css)
Creating a unique temporary file name
To create a temporary file with a .tmp file extension and returns the path to it, you use the GetTempFileName()
method. For example:
using static System.Console;
string tempFileName = Path.GetTempFileName();
WriteLine(tempFileName);
Code language: C# (cs)
The method creates a unique temporary file in the current user’s temporary directory. For example, on Windows, it outputs the following:
C:\Users\username\AppData\Local\Temp\tmpC430.tmp
Code language: C# (cs)
The username
is your current Windows user.
Getting a temporary directory path of the current user
To get the temporary path of the current user, you use the GetTempPath()
method. For example:
using static System.Console;
string tempDir = Path.GetTempPath();
WriteLine(tempDir);
Code language: C# (cs)
Example output on Windows:
C:\Users\username\AppData\Local\Temp\
Code language: C# (cs)
Changing the extension of the path
The ChangeExtension()
method changes the file extension of a path. The following program uses the ChangExtension()
method to change the .txt
to .csv
:
using static System.Console;
string path = @"C:\temp\readme.txt";
string newExtension = ".csv";
string newPath = Path.ChangeExtension(path, newExtension);
WriteLine(newPath);
Code language: C# (cs)
Checking if a path contains a file extension
The HasExtension
method returns true
if a path contains a file extension or false
otherwise:
using static System.Console;
string path = @"C:\temp\readme.txt";
bool hasExtension = Path.HasExtension(path);
WriteLine(hasExtension);
Code language: C# (cs)
Output:
True
Code language: C# (cs)
Summary
Method | Result |
---|---|
Path.GetFileName("C:\temp\readme.txt") | readme.txt |
Path.GetDirectoryName("C:\temp\readme.txt") | C:\temp |
Path.GetFullPath("..\..\readme.txt") | C:\project\bin\readme.txt |
Path.Combine(@"C:\temp", "readme.txt") | C:\temp\readme.txt \ |
Path.GetExtension(@"C:\temp\readme.txt") | .txt |
Path.GetTempFileName() | C:\Users\username\AppData\Local\Temp\tmpC430.tmp |
Path.GetTempPath() | C:\Users\username\AppData\Local\Temp\ |
Path.ChangeExtension(@"C:\temp\readme.txt", ".csv") | C:\temp\readme.csv |
Path.HasExtension(@"C:\temp\readme.txt" ) | True |