Rename File and Folder in C#, ASP.Net


There is no separate method for renaming the files and folders in c#. But we can achieve this by using Move method which is in File and Directory class.

Note: Please add System.IO namespace.

Renaming a File in C#

  string
path = Server.MapPath("~");
  string Fromfile = path + "\\[Foldername] \\" +[Filename];
  string Tofile = path + "\\[Foldername]\\" +[Filename];
      File.Move(Fromfile, Tofile);

Renaming a Folder in C#

       
string path = Server.MapPath("~") ;
        string Fromfol = "\\
[Foldername] \\";
        string Tofol = "\\[Foldername] \\";
        Directory.Move(path + Fromfol , path + Tofol);
Ebook Download
View all
Learn
View all