Subscribe to our YouTube Channel for more Videos

How to rename a file in C# ?


Want to rename a file using .NET Framework and C# ? , below is a sample code snippet that illustrates how to achieve the same .

How to rename a file in C# ?

using System;
using System.IO;

namespace GinktageConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            RenameFile(@"D:\Ginktage\file1.txt", @"D:\Ginktage\file2.txt");
            Console.ReadLine();
        }
        // Method to convert a byte array to stream in C#
        public static void RenameFile(string oldFileNamewithPath,string NewFileNamewithPath)
        {
            System.IO.File.Move(oldFileNamewithPath, NewFileNamewithPath);
        }
    }
    
}

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.