Changing file extention

Posted on 2006-04-03
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

    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);

    LVL 4

    Assisted Solution

    check Path.ChangeExtension method...

            string goodFileName = @"C:\mydir\";
            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\, '.old') returns 'C:\mydir\'
            // ChangeExtension(C:\mydir\, '') returns 'C:\mydir\'
            // ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'

    LVL 20

    Assisted Solution

    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));


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now