C# Path GetFileNameWithoutExtension() Method

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

Definition

In C#, Path.GetFileNameWithoutExtension() returns the file name part of a path without its last extension. For "/data/reports/2026.txt" the answer is "2026".

It first takes the last part of the path, as GetFileName does, and then cuts it at the last dot. Only the last dot counts, so archive.tar.gz becomes archive.tar. It works on text, so the file does not have to exist.

This is a static method of System.IO.Path. Call it as Path.GetFileNameWithoutExtension(...); 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, or just a file name.

C#
string stem = Path.GetFileNameWithoutExtension("/data/reports/2026.txt");

Console.WriteLine(stem);   // 2026
CallWhat it does
GetFileNameWithoutExtension("/a/b.txt")Returns b.
GetFileNameWithoutExtension("archive.tar.gz")Returns archive.tar. Only the last extension is removed.
GetFileNameWithoutExtension("Makefile")No dot. Returns Makefile.
GetFileNameWithoutExtension(".gitignore")The whole name counts as the extension. Returns an empty string.
GetFileNameWithoutExtension("/a/b/")Ends with a separator. Returns an empty string.

Combine it with Path.GetExtension to get both halves, and with Path.GetDirectoryName to build a sibling file name in the same folder.

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

Parameters

ParameterDescription
pathA path or a plain file name. It does not need to exist.

Return Value

Return ValueDescription
string?The last part of the path with its last extension (and the dot) removed. Empty for a trailing separator or a dot-only name, and null if path is null.

C# Examples

Example 1 — Strip the Extension: The Normal Case and the Edge Cases

See four names, including one with two dots and one with a leading dot.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        Console.WriteLine(Path.GetFileNameWithoutExtension("/data/reports/2026.txt"));
        Console.WriteLine(Path.GetFileNameWithoutExtension("archive.tar.gz"));
        Console.WriteLine(Path.GetFileNameWithoutExtension("Makefile"));
        Console.WriteLine("[" + Path.GetFileNameWithoutExtension(".gitignore") + "]");
    }
}

How It Works

  1. The normal case. The last part is 2026.txt. Cutting at the last dot leaves 2026. The folder part is never included.
  2. Two dots. Only the last extension is removed, so .tar stays and the answer is archive.tar.
  3. No dot. There is nothing to cut, so Makefile comes back as it is.
  4. A leading dot. In .gitignore the last dot is the first character, so the whole name is seen as the extension and the result is an empty string. The brackets make it visible.

Example 2 — Plan New Names With a Number

Use the name without extension, a counter, and the old extension to build a list of new file names.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] files = { "/pics/beach.jpg", "/pics/city.PNG", "/pics/forest.webp" };

        for (int i = 0; i < files.Length; i++) {
            string stem = Path.GetFileNameWithoutExtension(files[i]);
            string ext = Path.GetExtension(files[i]);
            string newName = stem + "-" + (i + 1).ToString("000") + ext;
            Console.WriteLine(newName);
        }
    }
}

How It Works

  1. Three sample paths. The array holds forward-slash paths, so they are split the same way on every system. Nothing needs to exist on disk.
  2. Split into two parts. GetFileNameWithoutExtension gives the stem and GetExtension gives the extension with its dot. Together they hold the whole file name.
  3. Build the new name. The loop index plus one is formatted as three digits by ToString("000"), so you get 001, 002, and 003.
  4. Case is kept. The extension of the second file stays .PNG. The code only plans new names. To rename for real, you would pass old and new paths to File.Move.

Example 3 — Build a Sibling File in the Same Folder

Take a path apart into folder and stem, then build a .bak path next to the original.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string file = "/data/reports/2026.txt";

        string folder = Path.GetDirectoryName(file);
        string stem = Path.GetFileNameWithoutExtension(file);
        string backup = Path.Combine(folder, stem + ".bak");

        Console.WriteLine(backup.Replace('\\', '/'));
        Console.WriteLine(Path.GetFileName(backup));
        Console.WriteLine(Path.GetDirectoryName(backup) == folder);
    }
}

How It Works

  1. Folder and stem. GetDirectoryName gives /data/reports (with a backslash style on Windows) and GetFileNameWithoutExtension gives 2026.
  2. Join them again. Combine puts the folder and the new file name together with a separator. Replace then makes the printed result the same on every system.
  3. Only the name changed. GetFileName of the new path is 2026.bak, so the old extension is gone and the new one is in place.
  4. Same folder. Asking for the folder of the backup path gives back exactly the string we started with, so the comparison is True.

Frequently Asked Questions

Call Path.GetFileNameWithoutExtension(path). For "/data/reports/2026.txt" it returns "2026". It works on the text, and the file does not need to exist.
It returns "archive.tar". Only the last dot counts, so the ".gz" part is removed and ".tar" stays in the name.
.NET treats everything from the last dot as the extension, and in ".gitignore" the last dot is the first character. So the extension is the whole name and nothing is left for the stem.
No. It first takes the last part of the path, like GetFileName, so no folder text is in the result. Use GetDirectoryName if you also need the folder.
Join the pieces: Path.GetFileNameWithoutExtension(file) + "_backup" + Path.GetExtension(file). Add Path.GetDirectoryName(file) with Path.Combine if the new file should stay in the same folder.

Next: GetFullPath()

Turn a relative path into a full one, and clean up dot segments such as .. along the way.

GetFullPath() 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.

13 people found this page helpful