C# Path GetFileName() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Path.GetFileName() returns the last part of a path: the text after the final separator. For "/data/reports/2026.txt" that is "2026.txt", with the extension included.

The method works on text only. It does not check that the file exists, and it does not care whether the last part is really a file or a folder. If the path ends with a separator, the last part is empty, so the answer is an empty string.

This is a static method of System.IO.Path. Call it as Path.GetFileName(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.

Syntax

Pass a path and read the result. A null path returns null.

C#
string name = Path.GetFileName("/data/reports/2026.txt");

Console.WriteLine(name);   // 2026.txt
CallWhat it does
GetFileName("/data/a.txt")Returns a.txt.
GetFileName("a.txt")No separator, so the whole text is the answer: a.txt.
GetFileName("/data/reports/")Ends with a separator. Returns an empty string.
GetFileName("/")Returns an empty string.
GetFileName(null)Returns null.

Use forward slashes in input when you want the same answer on every system. A backslash is a separator on Windows only; on Linux it is an ordinary character in the name.

Library declarations
C#
public static string? GetFileName(string? path);
public static ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path);

Parameters

ParameterDescription
pathA path string. It can be relative or absolute, and the file does not need to exist.

Return Value

Return ValueDescription
string?The characters after the last separator, including the extension. Empty if the path ends with a separator, and null if path is null.

C# Examples

Example 1 — Read the Last Part of a Path

Four forward-slash paths, including a plain name, a trailing separator, and a root.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.WriteLine(Path.GetFileName("/data/reports/2026.txt"));
        Console.WriteLine(Path.GetFileName("summary.txt"));
        Console.WriteLine("[" + Path.GetFileName("/data/reports/") + "]");
        Console.WriteLine("[" + Path.GetFileName("/") + "]");
    }
}

How It Works

  1. After the last separator. The text after the final / is 2026.txt, extension included. A forward slash works as a separator on every system.
  2. No separator at all. A plain name is returned whole, so summary.txt comes back unchanged.
  3. A trailing separator. Nothing follows the last slash of /data/reports/, so the answer is an empty string. The square brackets make it visible.
  4. A root. "/" is only a separator, so the answer is empty again. This is why you must not assume a non-empty result.

Example 2 — Read a Folder Name With TrimEndingDirectorySeparator

Fix the trailing-slash case by trimming first, then read the name of the folder.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] folders = { "/data/reports/", "/data/archive", "/var/log/" };

        foreach (string folder in folders) {
            string trimmed = Path.TrimEndingDirectorySeparator(folder);
            Console.WriteLine(folder + " -> " + Path.GetFileName(trimmed));
        }
    }
}

How It Works

  1. A mix of spellings. Two paths end with a separator and one does not. Without trimming, two of the three would print an empty name.
  2. Trim first. TrimEndingDirectorySeparator removes one trailing slash and leaves paths without one alone.
  3. Then ask for the last part. After trimming, the last part is always the folder name, so GetFileName returns reports, archive, and log.
  4. Print the pair. The loop prints the original and the name side by side. The input uses only forward slashes, so no Replace is needed here.

Example 3 — Compare GetFileName With FileInfo.Name

Create a small temp file and check that Path and FileInfo report the same name.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string file = Path.Combine(Path.GetTempPath(), "codetofun-filename-demo.txt");
        File.WriteAllText(file, "hello");

        string fromPath = Path.GetFileName(file);
        string fromInfo = new FileInfo(file).Name;

        Console.WriteLine(fromPath);
        Console.WriteLine("Same as FileInfo.Name: " + (fromPath == fromInfo));
        Console.WriteLine("Length: " + fromPath.Length);

        File.Delete(file);
    }
}

How It Works

  1. A real file for the demo. The file lives in the temp folder under a fixed name and is written with WriteAllText. The last line of the program deletes it.
  2. Two ways to the name. Path.GetFileName cuts the text. new FileInfo(file).Name asks the file system object for the same part.
  3. The same answer. Both give codetofun-filename-demo.txt, so the comparison prints True. The folder part of the path is never printed.
  4. Count the characters. The name has 27 characters: 23 in codetofun-filename-demo plus 4 in .txt. The length does not depend on where the temp folder is.

Frequently Asked Questions

Use Path.GetFileName(path). For "/data/reports/2026.txt" it returns "2026.txt". Use Path.GetFileNameWithoutExtension if you do not want the extension.
The path ends with a separator, so nothing follows it, or the path is a root such as "/". Trim the trailing separator first when the path names a folder.
Trim the trailing separator and call GetFileName: Path.GetFileName(Path.TrimEndingDirectorySeparator("/data/reports/")) gives "reports".
No. It only cuts the string after the last separator. The file can be missing, and the method still answers. Use File.Exists or Path.Exists to check the disk.
On Linux, a backslash is a normal character, not a separator. GetFileName("C:\\data\\a.txt") returns the whole string there. Convert the backslashes first, or take the path from the user in the system's own format.

Next: GetFileNameWithoutExtension()

Get the file name without its last extension: report.txt becomes report.

GetFileNameWithoutExtension() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful