Sometimes can rename, remove a folder; sometimes cannot
- by Vy Clarks
In my website project. I need to rename or remove some folder by code.
Sometimes I can do all of that, but sometimes I cannot with error: Access to the path is denied
Try to find to solution on Google. May be, there are two reason:
The permisstion of that Folder
Some subFolder or some file in that Folder that's being held open.
Try to check:
Right click on Folder- Properties-- Security: if this is the right way to check the permission, the Folder allowes every action (read, write....)
There are no file, no subfolder of that Folder is opened.
Why? I still dont understant why sometimes I can rename folder but sometimes I cannot.
Help!! I need your opinions!!!
UPDATE:
take a look at my code above:
I want to rename the a folder with the new name inputed in a Textbox txtFilenFolderName:
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:
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!