首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >File.Copy错误:由于文件系统限制,无法完成请求的操作

File.Copy错误:由于文件系统限制,无法完成请求的操作
EN

Stack Overflow用户
提问于 2013-03-28 13:40:41
回答 1查看 9.1K关注 0票数 7

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

有解决办法吗?

信息

  • 系统: Windows 2008 SP1 x64
  • 安装路径: http://support.microsoft.com/kb/967351/en-us?fr=1
  • Defragmentation:完成

代码语言:javascript
复制
    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;
        }
    }

Stacktrace

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-08 19:40:08

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

为了避免这个问题,我的文件总是用db行ID命名(这是一个guid)。

然后guid的每个部分都是一个目录,除了最后一个目录: ID为02510b5a-a605-4a4e-b00a-f554998378a9的文件存储在目录02510b5a/A 605/4a4e/b00a/中,并具有f554998378a9名称。所以我只需使用ID就可以直接到达文件,并且在许多目录中拆分了数百万个文件。

编辑:自从我在这里发布后,我注意到了关于我的解决方案的一个注释:在.NET中生成Guid,这样第一部分就会经常变化,而最后一部分不会经常(或很少)。使用上面描述的拆分会导致大量的第一级目录,然后每个子目录中只有一个子目录等等。因此,这仍然创建了大量的第一级目录,您也可以达到系统限制(我不知道限制在哪里,但Windows肯定不希望在同一个目录中有4000 000个子目录)。

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

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15683417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档