2013-03-28 31 views
6

構造内に4000000個以上のjpegファイルを取得した後、新しいファイルを追加する際に問題がありました。 File.Copy throw exception:ファイルシステムの制限により、要求された操作を完了できませんでした。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; 
     } 
    } 

STACKTRACE

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) 
+0

これは可能性の1つです。http://blogs.technet.com/b/seanearp/archive/2011/07/06/file-system-limitation.aspx –

+9

http://stackoverflow.com/questions/197162/ ntfs-performance-and-large-volumes-of-files-and-directories/291292#291292(_はあなたのための_です) –

答えて

2

あなたが問題を回避するために、あなたのディレクトリを分割し、分割しなければなりません。 Windowsは何百万ものファイルを持つディレクトリを好きではありません。

この問題を回避するため、私のファイルは常にdb行ID(guid)で名前が付けられています。
guidの各部分は、最後の部分を除いてディレクトリです。ID 02510b5a-a605-4a4e-b00a-f554998378a9のファイルはディレクトリ02510b5a/a605/4a4e/b00a /に格納され、名前はf554998378a9です。だから私はそのIDを使ってファイルに直接アクセスすることができ、何百万ものファイルが多くのディレクトリに分割されています。

EDIT: 私がここに投稿して以来、私の解決策に気付いただけです。.NETのGuidは、最初の部分が頻繁に変更され、最後の部分があまり頻繁に(またはまれに)変更されないように生成されます。上記のように分割を使用すると、第1レベルのディレクトリが多くなり、サブディレクトリごとにサブディレクトリが1つしかありません。このため、第1レベルのディレクトリがたくさん作成され、システムの制限にも達する可能性があります。制限はありますが、Windowsでは必ず同じディレクトリに4 000 000のサブディレクトリがありません)

解決策:解決策は、ディレクトリを作成するときにGuidの部分に戻すことです。

例:このGUID 02510b5a-a605-4a4e-b00a-f554998378a9のためにあなたはf554998378a9\b00a\4a4e\a605とファイル名02510b5a

あなたは、彼らがすべてのルックスがループのGUIDの何百万人を作成するので、もし.NET GUIDは、現在の時刻を使用して生成されていることに注意してくださいディレクトリを使用する必要があります同じように(ちょうど1番目の部分が異なります)、私のソリューションを使用して同じディレクトリに終わります。

+0

投稿したときに気づいた可能性のある問題について私の解決策を編集しました – Fabske

関連する問題