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.)
source
share