FileInfo - RenameExtension

Changes the extension of a specified file.

Try it

public static string FileName = "test.txt";

public static void Main()
{
	SaveFile();
	
	var file = FileName.ToFileInfo();
	Console.WriteLine("File Name: {0}", file.FullName);
	
	// C# Extension Method: FileInfo - RenameExtension
	file.RenameExtension("md");
	
	Console.WriteLine("New file Name: {0}", file.FullName);
}

private static void SaveFile()
{
	var html =
    @"<!DOCTYPE html>
<html>
<body>
<h1>This is <b>bold</b> heading</h1>
<p>This is <u>underlined</u> paragraph</p>
<h2>This is <i>italic</i> heading</h2>
</body>
</html> ";
	
	html.SaveAs(FileName);
}

View Source
using System;
using System.IO;

public static partial class Extensions
{
    /// <summary>
    ///     Changes the extension of a @this string.
    /// </summary>
    /// <param name="this">
    ///     The @this information to modify. The @this cannot contain any of the characters defined in
    ///     <see
    ///         cref="M:System.IO.Path.GetInvalidPathChars" />
    ///     .
    /// </param>
    /// <param name="extension">
    ///     The new extension (with or without a leading period). Specify null to remove an existing
    ///     extension from
    ///     <paramref
    ///         name="this" />
    ///     .
    /// </param>
    /// ###
    /// <exception cref="T:System.ArgumentException">
    ///     <paramref name="this" /> contains one or more of the invalid
    ///     characters defined in
    ///     <see
    ///         cref="M:System.IO.Path.GetInvalidPathChars" />
    ///     .
    /// </exception>
    public static void RenameExtension(this FileInfo @this, String extension)
    {
        string filePath = Path.ChangeExtension(@this.FullName, extension);
        @this.MoveTo(filePath);
    }
}