FileInfo - Rename
A FileInfo extension method that renames a file name.
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 - Rename file.Rename("newtest.txt"); 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.IO; public static partial class Extensions { /// <summary> /// A FileInfo extension method that renames. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="newName">Name of the new.</param> /// ### /// <returns>.</returns> public static void Rename(this FileInfo @this, string newName) { string filePath = Path.Combine(@this.Directory.FullName, newName); @this.MoveTo(filePath); } }