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!