0

I have a problem very curiosity.

I am delete files into folders this is the algoritm

            System.IO.DirectoryInfo di = new DirectoryInfo(folderPath);

            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true);
            }

I choose the directory and the algorithm deletes: folders, subfolders and everything related without problem.

The problem occurs when I download a compressed file, which has folders and subfolders (it is important to mention, that the data in the compressed file is what I should actually delete).

and the software reports an error: Access denied

The truth is that I do not understand, the files of that compressed (.zip) specifically some folders of that compressed file are the ones that will not let me delete.

Is there something missing in the algorithm that allows deletion?

jose angarita
  • 199
  • 1
  • 12
  • does your program have the appropriate permissions to make changes to the folder? – Glynn Hurrell May 04 '20 at 14:57
  • That is the doubt I have, I don't know if it is about permissions, but it strikes me that with some files it works with others, it doesn't. Could you guide me what are those permissions? – jose angarita May 04 '20 at 15:15
  • The windows file/folder permissions....https://learn.microsoft.com/en-us/windows/security/identity-protection/access-control/access-control – Glynn Hurrell May 04 '20 at 15:18
  • Oh right, in fact for the doubts thinking that could be that, I gave full permissions to read, write, modify each folder but it still throws the error at me. What I do not fully understand is because when I create manual files they are deleted without problem but when I decompress the .zip file it gives problems in some folders. The problem is that some folders are deleted and others give me the error. – jose angarita May 04 '20 at 15:21

1 Answers1

1

At first, to recursively delete folder with all files and subfolders you can just call Directory.Delete(folderPath, true). As described in documentation, you can have UnauthorizedAccessException if file or folder is protected. One of solutions you can do is to require your application to launch with admin privilegies as described in answer https://stackoverflow.com/a/2818776/10115818

  • Hello ... I have added the administrator permissions, I have added the manifest, I have done everything as indicated, the application has asked me for administrator credentials but it tells me: Access denied to the route – jose angarita May 04 '20 at 15:14
  • One else thing to try is to set Normal permissions for each file / directory with file.Attributes = FileAttributes.Normal; before file deletion and dir.Attributes = FileAttributes.Normal; before dir.Delete(true); – Andrew Kulikov May 04 '20 at 15:24
  • Could you give me a place where I can see an example of this to try? – jose angarita May 04 '20 at 15:28
  • In your first example it will look like this System.IO.DirectoryInfo di = new DirectoryInfo(folderPath); foreach (FileInfo file in di.GetFiles()) { file.Attributes = FileAttributes.Normal; file.Delete(); } foreach (DirectoryInfo dir in di.GetDirectories()) { dir.Attributes = FileAttributes.Normal; dir.Delete(true); } – Andrew Kulikov May 04 '20 at 15:33
  • Add the permissions but it still gives me error. There is something that I have realized. In the case of the tablet exactly with the folder with which I have the problem I just observed that there are 3 levels, that is: Main folder / secondary folder / third folder / files at this point gives me an error. The directory that I choose to delete is: Main folder / but on the tablet it gives me an error, in the other examples that I myself believe it does not give me an error. Is there a way to improve the algorithm so that it deletes the folders that are in other levels? – jose angarita May 04 '20 at 16:03
  • If indeed that is the problem, I have to replace the question: I have a main folder, this folder has 4 folders, inside each file there are: Main folder / folder 1 / folder 2 / folder 3 and folder 4 in folder 4 there are 4 more folders then: inside folder 1 there are many files, in folder 2 there are many files in folder 3 there are many files and in folder 4 there are 4 more folders and in each folder there are many files. The question is: Can this algorithm be improved to remove those folders, subfolders and files? – jose angarita May 04 '20 at 16:32
  • 1
    If I correctly understand, you need to do it recursively. This case you can see here https://gist.github.com/andrew-kulikov/c3555464ed35abe3b9287d85e8a069f9. Hope this will help. – Andrew Kulikov May 04 '20 at 19:21
  • thank u Andrew you solution is the solution. Only need resolved other situation. I need think how resolve it, if i some question i could i make a new publication. Thank u again. – jose angarita May 05 '20 at 12:42