Results 1 to 3 of 3

Thread: Obtaining Full Access To A Directory and Its Subdirectories/Files

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2010
    Posts
    56

    Obtaining Full Access To A Directory and Its Subdirectories/Files

    Hello, everyone! My application copies files and folders from the user's computer to a directory that the program itself creates. Later on in the application's lifespan, I need to delete this directory. However, whenever my program tries to delete this directory, it throws an exception that it has denied access to some of the files present in the directory. I have been searching for assistance, and all of the solutions that I found for setting directory access and file permissions haven't worked; I still encounter the same error after applying them.

    Here's the code I have for setting file/folder permissions and deleting the directory:

    Code:
    // Force us to get full access permissions to the directory and all of its subdirectories and files
    FileIOPermission FilePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, DestinationDir);
    
    FilePermission.Assert();
    
    // Delete the original folder
    Directory.Delete(DestinationDir, true);
    I have no problem with asserting the file permissions since the folder will be deleted anyone. I've also tried using the Demand() method but with no success.

    I've tried the following code as well:

    Code:
    // Force us to get full access permissions to the directory and all of its subdirectories and files
    DirectorySecurity DirSecurity = new DirectorySecurity(DestinationDir, AccessControlSections.All);
    
    DirSecurity.AddAccessRule(new FileSystemAccessRule(info.UserName, FileSystemRights.FullControl, AccessControlType.Allow));
    
    // Delete the original folder
    Directory.Delete(DestinationDir, true);
    None of these solutions have worked for me. I still receive an Access Denied error for one of the files inside the directory that I have created. My application is able to copy the file perfectly fine, but for some reason it cannot seem to delete it.

    And yes, my application has the requestedExecutionLevel set to requireAdministrator.

    Does anyone think he/she could help me out, please? Thanks in advance for any help!

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Obtaining Full Access To A Directory and Its Subdirectories/Files

    IIRC AccessDenied can be thrown because the file is locked as well as when you don't have permissions. Has your application perhaps loaded the file and not yet released its handle?

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2010
    Posts
    56

    Re: Obtaining Full Access To A Directory and Its Subdirectories/Files

    Quote Originally Posted by Evil_Giraffe View Post
    IIRC AccessDenied can be thrown because the file is locked as well as when you don't have permissions. Has your application perhaps loaded the file and not yet released its handle?
    Thanks for your input, Evil_Giraffe. Previously, my application had a handle on the directory that I would like to delete, but it only caused an error on a few particular files. I've now made sure that my application has released its handle on the directory, but the same files still give me an issue. Everything gets copied over fine; it's just that not everything gets deleted correctly.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width