Sometimes can rename, remove a folder; sometimes cannot

Posted by Vy Clarks on Stack Overflow See other posts from Stack Overflow or by Vy Clarks
Published on 2013-10-26T09:26:01Z Indexed on 2013/10/26 9:54 UTC
Read the original article Hit count: 273

Filed under:
|

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:

  1. The permisstion of that Folder
  2. Some subFolder or some file in that Folder that's being held open.

Try to check:

  1. Right click on Folder-> Properties--> Security: if this is the right way to check the permission, the Folder allowes every action (read, write....)
  2. 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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET