Skip to content

Instantly share code, notes, and snippets.

@maftieu
Last active August 29, 2015 14:11
Show Gist options
  • Save maftieu/f5c8f18488fcf6f3112b to your computer and use it in GitHub Desktop.
Save maftieu/f5c8f18488fcf6f3112b to your computer and use it in GitHub Desktop.
C# - Changes the extension of a file
/// <summary>
/// Changes the file extension by moving it.
/// </summary>
/// <param name="filePath">Full path to the file for which change the extension.</param>
/// <param name="extension">The new file extension.</param>
/// <returns>The path to the file, with the new extension.</returns>
private string ChangeFileExtension(string filePath, string extension)
{
if (!extension.StartsWith("."))
extension = "." + extension;
var d = Path.GetDirectoryName(filePath);
var fn = Path.GetFileNameWithoutExtension(filePath);
var newFp = Path.Combine(d, fn + extension);
File.Move(filePath, newFp);
return newFp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment