6

在结构中获得超过 4000000 个 jpeg 文件后,我们在添加新文件时遇到了问题。File.Copy 抛出异常:由于文件系统限制,请求的操作无法完成。

有什么解决办法吗?

信息

代码

    public bool AddFile(Uri uri, string path, bool withDelete = false)
    {
        var sourceFilePath = path;
        var destinationFilePath = Path.GetFullPath(uri.LocalPath);

        try
        {
            if (!File.Exists(sourceFilePath))
            {
                sourceFilePath = Directory.EnumerateFiles(sourceFilePath).FirstOrDefault();
                destinationFilePath = Path.Combine(destinationFilePath, Path.GetFileName(sourceFilePath));
            }

            if (!Directory.Exists(Path.GetDirectoryName(destinationFilePath)))
                Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));

            if (withDelete && File.Exists(destinationFilePath))
                File.Delete(destinationFilePath);

            File.Copy(sourceFilePath, destinationFilePath);

            return true;
        }
        catch (Exception exc)
        {
            ServiceCore.GetLogger().Error(exc);
            throw exc;
        }
    }

堆栈跟踪

    2013-03-28 14:10:48.3784[Info]: 47356388:Unive.NetService.SimpleServices.DocumentManagementSerivce..ctor: Entry
    2013-03-28 14:10:48.4740[Info]: Static:Unive.NetService.SimpleServices.DocumentManagementSerivce..ctor: Success
    2013-03-28 14:10:48.4899[Info]: 47356388:Unive.NetService.SimpleServices.DocumentManagementSerivce.UploadFile: Entry
    2013-03-28 14:11:26.3277[Error]: Exception
    Message:The requested operation could not be completed due to a file system limitation

    Source:mscorlib
    Stack Trace:   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
       at Unive.NetService.Business.SimpleFileClient.AddFile(Uri uri, String path, Boolean withDelete) in D:\Tag Prografix\Unive.NetService\Business\SimpleFileClient.cs:line 33
    TargetSite:Void WinIOError(Int32, System.String)
    2013-03-28 14:11:26.5029[Error]:                         47356388:Unive.NetService.SimpleServices.DocumentManagementSerivce.UploadFileException
    Message:The requested operation could not be completed due to a file system limitation

    Source:mscorlib
    Stack Trace:   at Unive.NetService.Business.SimpleFileClient.AddFile(Uri uri, String path, Boolean withDelete) in D:\Tag Prografix\Unive.NetService\Business\SimpleFileClient.cs:line 42
       at Unive.NetService.Business.FileService.UploadFile(Int64 fileId, String fileName, String path, Boolean isDiagram) in D:\Tag Prografix\Unive.NetService\Business\FileService.cs:line 80
       at Unive.NetService.SimpleServices.DocumentManagementSerivce.UploadFile(Int64 fileId, String fileName, String path) in D:\Tag Prografix\Unive.NetService\SimpleServices\DocumentManagementSerivce.asmx.cs:line 100
    TargetSite:Void WinIOError(Int32, System.String)
4

1 回答 1

2

您应该划分和拆分目录以避免出现问题。Windows 不喜欢包含数百万文件的目录。

为避免此问题,我的文件始终以 db 行 ID(即 guid)命名。
然后 guid 的每个部分都是一个目录,除了最后一个:ID 为 02510b5a-a605-4a4e-b00a-f554998378a9 的文件存储在目录 02510b5a/a605/4a4e/b00a/ 中,名称为 f554998378a9。所以我可以直接使用ID直接访问文件,数以百万计的文件被拆分在很多目录中。

编辑:自从我在这里发布以来,我注意到我的解决方案的一个评论:在 .NET Guid 中生成,因此第一部分经常更改,而最后一部分不那么频繁(或很少)。使用上述拆分会导致很多一级目录,然后每个子目录中只有一个子目录等。所以这仍然会创建很多一级目录,你也可能达到系统限制(我不知道在哪里限制,但 Windows 肯定不喜欢在同一目录中有 4 000 000 个子目录)

解决方案:解决方案是在创建目录时恢复为 Guid 部分。

示例:对于本指南02510b5a-a605-4a4e-b00a-f554998378a9,您应该使用目录f554998378a9\b00a\4a4e\a605和文件名02510b5a

请注意,.NET Guid 是使用当前时间生成的,因此如果您在循环中创建数百万个 Guid,它们看起来都一样(只是第一部分会有所不同)并使用我的解决方案最终在同一个目录中。

于 2013-04-08T19:40:08.350 回答