如果在Windows运行时中复制大文件,则会出现HRESULT E_FAIL错误

时间:2014-12-05 14:18:52

标签: c# windows-runtime windows-phone windows-store-apps windows-phone-8.1

当我尝试复制一个大文件(~500 MB)时收到此错误消息:

  

错误HRESULT E_FAIL已从调用COM组件返回。   HResult:-2147467259

这是我用来复制文件的代码:

await file.CopyAsync(storageFolder, name, NameCollisionOption.GenerateUniqueName);

感谢其背后的开发人员,它的信息量不大。

那么如何解决这个问题呢?感谢。

(我在Windows Phone 8.1中测试它)

1 个答案:

答案 0 :(得分:1)

我使用1.5 GB文件进行了测试,并能够复制该异常。手动设置内部缓冲区大小似乎可以解决问题,但要注意这会如何影响应用程序。 Read under the Buffer section on MSDN on dealing with large data sets.

CreateNewFileAsync()方法只是创建一个新文件:)

祝你好运,编码愉快。

    var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("largeFile.txt");

StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;

// Exception
if (file != null)
{
    StorageFile copiedFile = await file.CopyAsync(tempFolder, "copied.txt", NameCollisionOption.GenerateUniqueName);
}


// Setting the internal buffer to 1024 
// Be aware- from MSDN: However, this buffer is allocated on the large object heap 
// and could potentially degrade garbage collection performance. 
// You should only use large buffer sizes if it will noticeably improve the performance of your app.
var newFile = await CreateNewFileAsync();

using (Stream ss = await file.OpenStreamForReadAsync())
using (Stream sd = await newFile.OpenStreamForWriteAsync())
{
    await ss.CopyToAsync(sd, 1024);
    var fileProps = await file.GetBasicPropertiesAsync();
    var size = fileProps.Size;
}