Tuesday, January 10, 2012

How to Rename File and Folder in C#, ASP.Net


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);
}


Source: codedigest.com

No comments:

Post a Comment