CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Rename File and Folder in C#, ASP.Net
Submitted By Satheesh Babu B
On 11/2/2008 5:29:25 AM
Tags: asp.net,C#,CodeDigest  

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.

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..