复制大文件时如何避免 StorageFile.CopyAsync() 抛出异常?

编程入门 行业动态 更新时间:2024-10-27 14:31:52
本文介绍了复制大文件时如何避免 StorageFile.CopyAsync() 抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我将通过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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 13:23:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117775.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   大文件   异常   CopyAsync   StorageFile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!