Extension in einem Dateinamen / Pfad ändern
Bsp:
der Aufruf ChangeExtension(@"C:/Bilder/myImage.bmp", "jpg")
gibt folgenden String zurück: "C:/Bilder/myImage.jpg"
benötigter namespace:
System.Text.RegularExpressions
/// <summary>
/// changes the extension of the given filename
/// </summary>
/// <param name="path">the filename</param>
/// <param name="newExtension">the new extension</param>
/// <returns>filename with new extension</returns>
private static string ChangeExtension(string path, string newExtension)
{
return Regex.Replace(path, @"\.[^.]*$", "." + newExtension);
}
Kommentare zum Snippet