Change a File’s Extension in C#

This is a quick post about how to change the extension in a path. Note: This does NOT change the extension of the file. It will have to be moved for that to happen.

[code lang=”c#”]
System.IO.Path.GetDirectoryName(strFile) + System.IO.Path.DirectorySeparatorChar + System.IO.Path.GetFileNameWithoutExtension(strFile) + “.txt”;

4 thoughts on “Change a File’s Extension in C#

  1. Hi,
    I have a lot of files in a shared folder. I am trying to rename extensions of those files. For example i have 1.pdf.kayityok , 2.pdf.kayityok , 3.pdf.kayityok ; what i want to do is i want to rename the extensions as 1.pdf , 2.pdf , 3.pdf. I havent done coding in around 1,5 years so i am a lot rusty so if you can give detailed explanations i will be glad.

    • Murat,
      You might be better off posting your question to or because it depends a lot on the type of environment that you have. Are you using Linux, Windows, or OSX Servers. What programming languages are you comfortable in. etc.

Leave a Reply

Your email address will not be published. Required fields are marked *