HOWTO · Csharp
How to Get File Name From the Path in C#
Learn how to use C# path APIs to get a file name from a path, handle platform-specific separators, and distinguish file paths from URLs.
On this page
To retrieve the last component of a filesystem path in C#, use Path.GetFileName from System.IO. It returns the file name together with its extension, so you do not need to split the path manually. The method extracts text from a path string; it does not open or check the file.
Use Path.GetFileName to get the final path component
Pass the path string to Path.GetFileName. The result is the final component after the directory separator recognized by the current operating system. For a path ending in summary.csv, the returned name is summary.csv.
Run this example:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "reports/2026/summary.csv";
string fileName = Path.GetFileName(path);
Console.WriteLine(fileName);
}
}
summary.csv
This program writes the extracted name to the console. The code is self-contained and uses a slash-separated path, which is recognized as a directory separator on common .NET platforms.
Handle absolute, relative, and platform-specific paths
The method works on both absolute and relative filesystem paths. An absolute path includes its location from a root or drive; a relative path is interpreted from the application’s working directory. Path.GetFileName does not resolve either path against the file system: it only returns the final component of the supplied string.
Separator handling follows the platform-specific rules of Path. A slash-separated path such as reports/summary.csv works on Unix-like systems and Windows. A backslash-separated Windows path such as C:\Reports\summary.csv is interpreted as intended on Windows. On Unix-like systems, a backslash is an ordinary character, so passing a Windows-formatted path there may return the entire string instead of only summary.csv. If an application processes paths created for another operating system, account for that format explicitly rather than assuming the host platform will parse it the same way.
Get the file name without its extension
When the caller needs summary rather than summary.csv, use Path.GetFileNameWithoutExtension. It combines the same final-component extraction with removal of the final extension. For example, given reports/summary.csv on a platform that recognizes / as a separator, it returns summary.
Use this API when the extension is not part of the value you need to display or compare. If you need the extension separately, Path.GetExtension returns it, including the leading period. These methods operate on path text and do not determine whether a file exists.
Distinguish file paths from URLs and file validation
A URL is not necessarily a filesystem path. It can contain a scheme, host, query, or fragment, and URL path segments follow URL rules. Do not pass a URL to Path.GetFileName as a substitute for URL parsing; use a URI parser when the input is a URL, then handle any decoded name according to the application’s needs.
Extracting a name also does not validate a path, prove that it is safe to use, or check that a file exists. Those are separate tasks. Use the appropriate path validation and file-system APIs for the operation, and apply the application’s security rules before opening or creating files. For the narrower task of obtaining the last component from a filesystem path string, Path.GetFileName is the direct API.