复制大文件时如何避免 StorageFile.CopyAsync() 抛出异常? [英] How to avoid StorageFile.CopyAsync() throw exception when copying big file?

查看:31
本文介绍了复制大文件时如何避免 StorageFile.CopyAsync() 抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将通过StorageFile.CopyAsync()方法将视频库中的一些文件复制到我的应用程序存储中,但如果文件大小超过1GB,则会引发如下异常:

I'm going to copy some files from Video Library to my app storage through StorageFile.CopyAsync() method, but if a file's size is more than 1GB, it would throw an exception as follow:

类型:System.Runtime.InteropServices.COMException 消息:错误HRESULT E_FAIL 已从对 COM 组件的调用返回.堆栈跟踪:在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

Type: System.Runtime.InteropServices.COMException Message: Error HRESULT E_FAIL has been returned from a call to a COM component. Stacktrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

如何导入一个大文件,有没有办法解决这个问题?

How can I import a big file, Is there have a solution to solve this problem?

推荐答案

我会尝试通过缓冲区复制它 - 例如像这样:

I would try to copy it via buffer - for example like this:

private async Task CopyBigFile(StorageFile fileSource, StorageFile fileDest, CancellationToken ct)
{
   using (Stream streamSource = await fileSource.OpenStreamForReadAsync())
   using (Stream streamDest = await fileDest.OpenStreamForWriteAsync())
       await streamSource.CopyToAsync(streamDest, 1024, ct);
   return;
}

这篇关于复制大文件时如何避免 StorageFile.CopyAsync() 抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆