在结构中获得超过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;
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
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)
Run Code Online (Sandbox Code Playgroud)
您应该划分目录以避免出现问题。Windows 不喜欢包含数百万个文件的目录。
为了避免这个问题,我的文件总是以数据库行 ID(这是一个 guid)命名。
然后guid的每个部分都是一个目录,除了最后一个:ID为02510b5a-a605-4a4e-b00a-f554998378a9的文件存储在目录02510b5a/a605/4a4e/b00a/中,名称为f554998378a9。所以我可以直接使用 ID 来访问文件,数百万个文件被分割在很多目录中。
编辑:自从我在这里发布以来,我注意到我的解决方案:在 .NET Guid 中生成,以便第一部分经常更改,而最后一部分不那么频繁(或很少)更改。使用如上所述的分割会导致很多一级目录,然后每个子目录中只有 1 个子目录等。所以这仍然会创建很多一级目录,并且您也可能达到系统限制(我不知道在哪里)限制,但 Windows 肯定不喜欢同一目录中有 4 000 000 个子目录)
解决方案:解决方案是在创建目录时恢复为Guid部分。
示例:对于此 Guid,02510b5a-a605-4a4e-b00a-f554998378a9
您应该使用目录f554998378a9\b00a\4a4e\a605
和文件名02510b5a
请注意,.NET Guid 是使用当前时间生成的,因此,如果您在循环中创建数百万个 Guid,它们看起来都一样(只是第一部分不同),并最终使用我的解决方案位于同一目录中。
归档时间: |
|
查看次数: |
4841 次 |
最近记录: |