简体   繁体   English

如何使用 C# 从包含大量其他文件或文件夹的文件夹中正确删除 .Net Core 中的文件

[英]How to properly delete a file in .Net Core with C# from a folder with large amount of other files or folders

I am removing files in a loop with File.Delete(String) in a simplest .Net Core Console application:我在最简单的 .Net Core 控制台应用程序中使用File.Delete(String)在循环中删除文件:

1. foreach (var listing in listings)
2.    File.Delete(listing.Path);

BUT the issue is that the application randomly hangs on line 2, no exception is thrown .问题是应用程序随机挂在第 2 行,没有抛出异常 After application restart, several files are removed successfully and the app randomly hangs, again, on another file.应用程序重启后,几个文件被成功删除,应用程序再次随机挂在另一个文件上。

There are some items to note:有几点需要注意:

  • listing.Path is an absolute path to a file on a disk listing.Path是磁盘上文件的绝对路径
  • listing.Path looks like D:\\Output\\00000001\\6b1c8e6c-7d83-481a-a7db-aac9024059c4.png listing.Path看起来像D:\\Output\\00000001\\6b1c8e6c-7d83-481a-a7db-aac9024059c4.png
  • the length of listings array is about 10 items listings数组的长度约为10
  • file DOES exist in every case文件存在在任何情况下
  • Storage: Seagate Exos 7E8 512E 8TB 7200rpm 256MB ST8000NM000A 3.5" SATA III HDD存储:希捷 E​​xos 7E8 512E 8TB 7200rpm 256MB ST8000NM000A 3.5" SATA III硬盘
  • OS: Windows 10 64x操作系统:Windows 10 64x
  • total size of Output folder is 210GB Output文件夹的总大小为210GB
  • Output folder contains 137 000 other folders with 928 000 files in total (approximately 5 - 10 files per folder) Output文件夹包含137 000 个其他文件夹,总共928 000 个文件(每个文件夹大约5 - 10个文件)
  • average file size is about 350KB平均文件大小约为350KB

I suspect that issue is somehow related to the amount of files.我怀疑这个问题与文件数量有关。 But File.Delete should be the most reliable way to remove a file, and it just hangs without any response for multiple hours.但是File.Delete应该是删除文件最可靠的方法,它只是挂起几个小时没有任何响应。 Which forces me to kill the process of the app.这迫使我终止应用程序的进程。 Is there a better or more efficient way to remove a file?有没有更好或更有效的方法来删除文件?

Update: After moving all the files to another Windows machine in a network with an SSD installed and executing the application there it worked perfectly fine.更新:将所有文件移动到安装了SSD的网络中的另一台 Windows 机器并在那里执行应用程序后,它运行得非常好。 So it may look like a hardware issue.所以它可能看起来像一个硬件问题。 Therefore I will rephrase the question title from How to properly delete a file in .Net Core with C# to How to properly delete a file in .Net Core with C# from a folder with large amount of other files or folders因此,我将问题标题从“如何使用 C#正确删除 .Net Core 中的文件”改为“如何使用 C# 从包含大量其他文件或文件夹的文件夹中正确删除 .Net Core 中的文件”

1) Don't expect to create an array with hundreds of thousands of filenames and then loop through them deleting them very fast 1)不要指望创建一个包含数十万个文件名的数组,然后循环遍历它们并非常快地删除它们

2) The file system, especially if the directory is on a network share, mapped drive, or in use by another application will be slow and block operations. 2) 文件系统,特别是当目录位于网络共享、映射驱动器或被其他应用程序使用时,将会很慢并阻止操作。 For example, a processing adding new files to a directory as fast as possible will conflict and possibly deadlock with one deleting files as fast as possible from the same directory.例如,尽可能快地将新文件添加到目录的处理将与尽快从同一目录中删除文件发生冲突并可能死锁。

3) Files may be locked by another process 3) 文件可能被另一个进程锁定

Try some tests a) How long does it take to get a list of all of the files to be deleted?尝试一些测试 a) 获取要删除的所有文件的列表需要多长时间? b) How long does it take to delete 1000 files in a row? b) 连续删除 1000 个文件需要多长时间?

One queue system I developed had large numbers of files with multiple processes creating files, multiple processes processing files and then deleting the files.我开发的一个队列系统有大量文件,其中多个进程创建文件,多个进程处理文件,然后删除文件。

We had 3 directories: Inbound, BeingProcessed, and ForDeletion我们有 3 个目录:Inbound、BeingProcessed 和 ForDeletion

Each new file was put in the Inbound directory Another process would move the file from the inbound directory to the BeingProcessed directory, open and lock the file, then process the file and the move the file to the ForDeletion directory Another process would delete files in the ForDeletion directory每个新文件都放在 Inbound 目录中 另一个进程会将文件从 inbound 目录移动到 BeingProcessed 目录,打开并锁定文件,然后处理该文件并将文件移动到 ForDeletion 目录 另一个进程将删除文件中的文件删除目录

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

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