简体   繁体   English

C#:FileStream.SetLength(long)失败,并显示“由于文件系统限制,请求的操作无法完成”(在Windows 10 Home,NTFS上)

[英]C#: FileStream.SetLength(long) fails with “The requested operation could not be completed due to a file system limitation” (on Windows 10 Home, NTFS)

I had a very confusing experience with the System.IO.FileStream when writing a fairly large file that was approaching 8Gb in size***. 编写相当大的文件,大小接近8Gb时,我对System.IO.FileStream感到非常困惑。 The call to FileStream.SetLength(LARGE_NUMBER) started to suddenly fail with the error 对FileStream.SetLength(LARGE_NUMBER)的调用开始突然因错误而失败

The requested operation could not be completed due to a file system limitation 由于文件系统限制,请求的操作无法完成

The stacktrace was: stacktrace是:

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.SetLengthCore(Int64 value)
   at System.IO.FileStream.SetLength(Int64 value)
   at LiteDB.FileDiskService.WriteJournal(ICollection`1 pages, UInt32 lastPageID)
   ...

This was reproducible and fairly puzzling as googling turned up no obvious answer. 由于谷歌搜索没有明显的答案,因此这是可重复的并且相当令人困惑。

For the record, the LARGE_NUMBER SetLength was being called with was around 8,110,000,000 -- not small, but not in any way special. 记录下来,被调用的LARGE_NUMBER SetLength约为8,110,000,000,虽然不小,但没有任何特殊之处。 I've certainly seen larger files on NTFS before. 我以前肯定在NTFS上看到过较大的文件。


*** It's a LiteDB database file -- I do recognise I'm probably stretching what it is meant to be used for but this isn't the point of the question. ***这是一个LiteDB数据库文件-我确实知道我可能正在扩展它的用途,但这不是问题的重点。

While I cannot say I have found the exact explanation of what the problem was, I managed to resolve the problem and enable my program to continue extending this file. 虽然我不能说我找到了问题的确切解释,但我设法解决了问题并使我的程序能够继续扩展此文件。

I cannot say if this was related, but I noticed a line at https://support.microsoft.com/en-us/help/967351/a-heavily-fragmented-file-in-an-ntfs-volume-may-not-grow-beyond-a-cert saying the following: 我不能说这是否相关,但是我在https://support.microsoft.com/en-us/help/967351/a-heavily-fragmented-file-in-an-ntfs-volume-may-不能超过证明以下内容:

A heavily fragmented file in an NTFS file system volume may not grow beyond a certain size caused by an implementation limit in structures that are used to describe the allocations. 由于用于描述分配的结构中的实现限制,NTFS文件系统卷中的碎片过多的文件可能不会增长到超过一定大小。

Since I had no other ideas left, I thought I'd try simply making a copy of the file and hoping that the copy would end up less fragmented than a file that was written very gradually in small increments over a long time. 由于我没有其他想法了,因此我想我会尝试简单地制作该文件的副本,并希望该副本的碎片化程度要比长期以来以很小的增量逐渐逐渐写入的文件少。 Amazingly, this worked, and FileStream.SetLength(LARGE_NUMBER) on the copy of the old file started working again. 令人惊讶的是,这有效,并且旧文件副本上的FileStream.SetLength(LARGE_NUMBER)再次开始工作。

Interestingly, before I made a copy, I tried renaming the old problematic file and I could not do even that, getting a Windows error popup saying the same thing about the file system limitation. 有趣的是,在制作副本之前,我尝试重命名有问题的旧文件,但我什至无法做到这一点,出现Windows错误弹出窗口,提示有关文件系统限制的问题。

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

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