简体   繁体   English

将文件删除到文件夹 c#

[英]Delete files into a folder c#

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.事实是我不明白,该压缩文件(.zip)的文件,特别是该压缩文件的某些文件夹是不允许我删除的。

Is there something missing in the algorithm that allows deletion?允许删除的算法中是否缺少某些内容?

At first, to recursively delete folder with all files and subfolders you can just call Directory.Delete(folderPath, true) .首先,要递归删除包含所有文件和子文件夹的文件夹,您只需调用Directory.Delete(folderPath, true) As described in documentation , you can have UnauthorizedAccessException if file or folder is protected.文档中所述,如果文件或文件夹受到保护,您可能会遇到UnauthorizedAccessException 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您可以做的一种解决方案是要求您的应用程序以管理员权限启动,如答案https://stackoverflow.com/a/2818776/10115818中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM