Changing file extention

Posted on 2006-04-03
Medium Priority
Last Modified: 2008-01-09
I have to change extension of a lot of files. What is the easiest solution. Is there a property something like FileExtention that can be changed? Thanks.
Question by:quasar_ee
LVL 96

Accepted Solution

Bob Learned earned 172 total points
ID: 16366997
Here's one way:

using System.IO;


string sourceFile = @"c:\temp\test.xml";
string filePath = Path.GetDirectoryName(sourceFile);
string fileName = Path.GetFileNameWithoutExtension(sourceFile);
string targetFile = Path.Combine(filePath, fileName) + ".config";
File.Move(sourceFile, targetFile);


Assisted Solution

msreekm earned 164 total points
ID: 16367329
check Path.ChangeExtension method...

        string goodFileName = @"C:\mydir\myfile.com.extension";
        string badFileName = @"C:\mydir\";
        string result;

        result = Path.ChangeExtension(goodFileName, ".old");
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
            goodFileName, result);

        result = Path.ChangeExtension(goodFileName, "");
        Console.WriteLine("ChangeExtension({0}, '') returns '{1}'",
            goodFileName, result);

        result = Path.ChangeExtension(badFileName, ".old");
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
            badFileName, result);

        // This code produces output similar to the following:
        // ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
        // ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
        // ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'

LVL 20

Assisted Solution

TheAvenger earned 164 total points
ID: 16367941
The solution of Bob works fine. The solution of msreekm has the chance to be the shorter one but he missed that ChangeExtension only changes it in the path string, does not change the extension of the file. So I'll combine the two:

string sourceFile = @"c:\temp\test.xml";
string newExtension = "config";
File.Move(sourceFile, Path.ChangeExtension (sourceFile, newExtension));


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question