Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to rename the a folder with the new name inputed in a Textbox
txtFilenFolderName


C#
protected void btnUpdate_Click(object sender, EventArgs e)
   {
           string[] values = EditValue;

           string oldpath = values[0];// = "D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder"

           string oldName = values[2]; //= New Folder

           string newName = txtFilenFolderName.Text; //= New Folder1
           string newPath = string.Empty;


           if (oldName != newName)
           {
                   newPath = oldpath.Replace(oldName, newName);
                   Directory.Move(oldpath, newPath);
           }
           else
                lblmessage2.Text = "New name must not be the same as the old ";
       }
   }


Try to debug:

VB
oldpath = "D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder"
 oldName = New Folder
newName= New Folder1
newpath = "D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder1"


Everything seems right, but I when I click on buton Edit ---> rename---> Update---> an error occur:
Access to the path is denied D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder

Help!!!
Posted
Comments
That means the folder does not have permission to access it. Check it's properties and try to find the permissions inside it.
ZurdoDev 25-Oct-13 12:09pm    
Post as solution. Perhaps mention the identity of the app pool needs permissions.
Not sure, but OP says it has all the permissions. See below comment.
Vy Clarks 25-Oct-13 12:26pm    
The permission is allow every action.

You either don't have permissions to rename the folder or there's a file somewhere in that folder or its subfolders that's being held open. You cannot rename any part of a folderpath if there is a file open in it.
 
Share this answer
 
Comments
Vy Clarks 25-Oct-13 22:47pm    
yes, because New Folder still has a subfolder inside. So that I cannot rename folder which has subfolder inside?????
Dave Kreskowiak 25-Oct-13 23:08pm    
No, I said you cannot rename a any part of a folder path that has a FILE BEING HELD OPEN.
Your directory move method call should be inside a try catch block

C#
try
{
  Directory.Move(oldpath, newPath);
}
catch(UnauthorizedAccessException  ex)
{
   //User have no permission to move directory from source to destination location.
}
carch(Exception e)
{
   //all others exception.
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900