1

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!

JERRY-the chuha
  • 592
  • 1
  • 4
  • 16
vyclarks
  • 854
  • 2
  • 15
  • 39

2 Answers2

2

Grant permission(read/write/modify) to the user you are logged in as. Or you can run your program 'as Administrator'.

Edit: Try this.. Add reference to Microsoft.VisualBasic in your application. Then use

Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory("currentName", "newName");

Hope it helps :)

P.S.: RenameDirectory validate parameters and call Directory.Move. If you want to rename folder in server, you can grant modify access in root folder to IIS users. But if you intend to rename folders in client machine, you cant just rename any folder(except a few directories like a folder in temp).

Avishek
  • 1,896
  • 14
  • 33
0

Generally a website does not simply have access to a clients local file system. (And that is a good thing too!)

Refer for instance to this post: How to get client file system directories in treeview asp.net C#

Community
  • 1
  • 1
oerkelens
  • 5,053
  • 1
  • 22
  • 29