Monday, October 11, 2010

How to rename folder in c#?

Here I'm showing how to rename a folder or Directory in C#. Before going to the code we need some dll to add in our project. So we have to add reference to Microsoft.VisualBasic.dll
 
Here the code to rename the folder

using Microsoft.VisualBasic.MyServices; 


 try
        {
            FileSystemProxy FileSystem = new Microsoft.VisualBasic.Devices.Computer().FileSystem;
            // FileSystem.RenameDirectory("original folder path", "new name");
            FileSystem.RenameDirectory(Server.MapPath("~/bishnu"), "Bishnu1"); //this will rename bishnu to Bishnu1
            FileSystem = null;
        }
        catch (Exception ex) { }