C# Path ChangeExtension() Method

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

Definition

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
CallWhat 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

ParameterDescription
pathThe path to change. It may be null, and the method then returns null.
extensionThe new extension, with or without a leading dot. null removes the current extension.

Return Value

Return ValueDescription
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));
    }
}

How It Works

  1. 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.
  2. The dot is optional. "pdf" has no leading dot, so the method adds one and the result ends in .pdf.
  3. Add. /data/report had no extension, so .csv is simply appended.
  4. 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);
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.
  4. 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.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string oldPath = Path.Combine(Path.GetTempPath(), "codetofun-ext-demo.txt");
        string newPath = Path.ChangeExtension(oldPath, ".md");

        if (File.Exists(oldPath)) File.Delete(oldPath);
        if (File.Exists(newPath)) File.Delete(newPath);
        File.WriteAllText(oldPath, "hello");

        Console.WriteLine("Old exists: " + File.Exists(oldPath));
        Console.WriteLine("New exists: " + File.Exists(newPath));
        Console.WriteLine("New name: " + Path.GetFileName(newPath));

        File.Move(oldPath, newPath);
        Console.WriteLine("After Move, old: " + File.Exists(oldPath) + ", new: " + File.Exists(newPath));

        File.Delete(newPath);
    }
}

How It Works

  1. 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.
  2. 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.
  3. Read the new name. GetFileName(newPath) shows the changed file name only, so no machine-specific folder is printed.
  4. 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.

Next: Combine()

Join folders and a file name into one path, with the right separator for your system.

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

7 people found this page helpful