Last active
August 29, 2015 14:11
-
-
Save maftieu/f5c8f18488fcf6f3112b to your computer and use it in GitHub Desktop.
C# - Changes the extension of a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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