In C#, Path.ChangeExtension() takes a path and returns a new string whose extension is replaced. "/data/report.txt" with ".md" becomes "/data/report.md". If the path has no extension, the new one is added at the end.
It is text only. It does not rename the file on disk. To really rename, compute the new name with ChangeExtension and then call File.Move.
This is a static method of System.IO.Path. Call it as Path.ChangeExtension(...); 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 the path and the new extension. A leading dot on the extension is optional.
C#
string a = Path.ChangeExtension("/data/report.txt", ".md"); // /data/report.md
string b = Path.ChangeExtension("/data/report.txt", "pdf"); // /data/report.pdf
string c = Path.ChangeExtension("/data/report.txt", null); // /data/report
Call
What it does
ChangeExtension("a.txt", ".md")
Replaces the extension. Result a.md.
ChangeExtension("a.txt", "md")
The dot is added for you. Result a.md.
ChangeExtension("a.txt", null)
Removes the extension. Result a.
ChangeExtension("a", ".md")
The path had none, so the extension is added. Result a.md.
ChangeExtension("a.txt", "")
An empty string keeps the dot: the result is a..
Only the last extension changes: archive.tar.gz with ".zip" gives archive.tar.zip. A dot in a folder name is ignored, because the search stops at the last separator.
Library declarations
C#
public static string? ChangeExtension(string? path, string? extension);
Parameters
Parameter
Description
path
The path to change. It may be null, and the method then returns null.
extension
The new extension, with or without a leading dot. null removes the current extension.
Return Value
Return Value
Description
string?
The path with the new extension, or null if path is null. The original string is not changed.
C# Examples
Example 1 — Replace, Add, and Remove an Extension
Try the three most common changes on forward-slash paths. They give the same text on every system.
C#
using System;
using System.IO;
class Program {
static void Main() {
Console.WriteLine(Path.ChangeExtension("/data/report.txt", ".md"));
Console.WriteLine(Path.ChangeExtension("/data/report.txt", "pdf"));
Console.WriteLine(Path.ChangeExtension("/data/report", ".csv"));
Console.WriteLine(Path.ChangeExtension("/data/report.txt", null));
}
}
Replace..txt becomes .md. The folder part is copied as written. ChangeExtension does not change the separators, so the answer is the same on Windows and Linux.
The dot is optional."pdf" has no leading dot, so the method adds one and the result ends in .pdf.
Add./data/report had no extension, so .csv is simply appended.
Remove. A null extension cuts the extension off, together with its dot, and leaves /data/report.
Example 2 — Dots in Names and Folders
Several dots, a dot in a folder name, and a null path: see which dot ChangeExtension picks.
C#
using System;
using System.IO;
class Program {
static void Main() {
Console.WriteLine(Path.ChangeExtension("/data/archive.tar.gz", ".zip"));
Console.WriteLine(Path.ChangeExtension("/data/v1.2/readme", ".txt"));
Console.WriteLine(Path.ChangeExtension("/data/notes.txt", ""));
string missing = null;
string result = Path.ChangeExtension(missing, ".txt");
Console.WriteLine(result == null);
}
}
Last dot only.archive.tar.gz has two dots. The method works from the right, finds the dot before gz, and replaces only that part.
The folder dot is skipped. The search hits the separator before the file name readme and stops, so the dot in v1.2 is not an extension. The new one is appended.
An empty extension keeps the dot. The old extension is cut off and a dot with nothing after it is added, which gives notes.. Use null when you really want it gone.
null in, null out. A null path is returned as null, with no exception, so the comparison is True.
Example 3 — ChangeExtension Does Not Rename: File.Move Does
Create a small file in the temp folder, compute the new path, and show that the disk only changes after File.Move.
Old exists: True
New exists: False
New name: codetofun-ext-demo.md
After Move, old: False, new: True
How It Works
Two paths, one file.oldPath comes from GetTempPath, and newPath is the same text with .md. The two lines before the write remove leftovers from an earlier run.
Only the old one is real. After WriteAllText, the old path exists and the new one does not. Computing newPath changed nothing on the disk.
Read the new name.GetFileName(newPath) shows the changed file name only, so no machine-specific folder is printed.
Now the rename is real.File.Move moves the file to the new path, so the old path is gone and the new one exists. The last line deletes the demo file.
Frequently Asked Questions
Call Path.ChangeExtension(path, ".new") to get the new path text. To rename the actual file, pass the old and new paths to File.Move.
No. It only returns a string. The file on disk keeps its old name until you call File.Move or File.Copy with the new path.
Pass null as the extension. Path.ChangeExtension("/data/report.txt", null) returns "/data/report". An empty string is different: it leaves a dot at the end.
No. Both ".md" and "md" give the same result, because ChangeExtension adds a dot when the extension does not start with one.
Only the last extension is replaced, so changing it to ".zip" gives archive.tar.zip. If you need to remove both parts, call GetFileNameWithoutExtension twice or trim the name yourself.