Rename File and Folder in C#, ASP.Net
There is no separate Rename method for renaming a file and a folder. It can be achieved by Move method packed with File and Directory class.
The below code snippets will help us doing this.
Renaming a File in C#
protected void btnRenameFile_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~") + "\\test\\";
string Fromfile = path + "FileLength.txt";
string Tofile = path + "FileLength1.txt";
File.Move(Fromfile, Tofile);
}
Rename a Folder in C#
protected void btnRenameFolder_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~") ;
string Fromfol = "\\test\\";
string Tofol = "\\test1\\";
Directory.Move(path + Fromfol, path + Tofol);
}
Thus, we can rename files and folders in asp.net application with these code snippets.
We need to include System.IO namespace for this code to work.
|