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.
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
Parameter
Description
path
A path or a plain file name. It does not need to exist.
Return Value
Return Value
Description
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") + "]");
}
}
Output
2026
archive.tar
Makefile
[]
How It Works
The normal case. The last part is 2026.txt. Cutting at the last dot leaves 2026. The folder part is never included.
Two dots. Only the last extension is removed, so .tar stays and the answer is archive.tar.
No dot. There is nothing to cut, so Makefile comes back as it is.
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);
}
}
}
Output
beach-001.jpg
city-002.PNG
forest-003.webp
How It Works
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.
Split into two parts.GetFileNameWithoutExtension gives the stem and GetExtension gives the extension with its dot. Together they hold the whole file name.
Build the new name. The loop index plus one is formatted as three digits by ToString("000"), so you get 001, 002, and 003.
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);
}
}
Output
/data/reports/2026.bak
2026.bak
True
How It Works
Folder and stem.GetDirectoryName gives /data/reports (with a backslash style on Windows) and GetFileNameWithoutExtension gives 2026.
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.
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.
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.