C # .Net 3.5 Code to replace file extension using LINQ

I wrote this very simple function to replace the file extension using LINQ in C # .NET 3.5, however I have the feeling that there is a more elegant way to do this. (I do not intend to use LINQ here - just looking for a more elegant approach.) Ideas?

    private string ReplaceFileExtension(string fileName, string newExtension)
    {
        string[] dotSplit = fileName.Split('.');
        return String.Join(".", dotSplit.Take(dotSplit.Length - 1).ToArray()) + "." + newExtension;
    }

(I know this will not work if the original file name does not have a period.)

+3
source share
1 answer

It is very simple ... just use System.IO.Path.ChangeExtension

+16
source

Source: https://habr.com/ru/post/1696685/


All Articles