UWP:byte []到文件

tes*_*ing 4 c# windows-phone win-universal-app windows-10-mobile

我有一个byte[],我想将它存储到一个文件中.这是我的代码:

using System.Runtime.InteropServices.WindowsRuntime;

StorageFolder folder = await GetStorageFolderFromFilePath(filePath);
StorageFile file = await folder.CreateFileAsync(Path.GetFileName(filePath), CreationCollisionOption.ReplaceExisting);

using (Stream stream = await file.OpenStreamForWriteAsync())
{
    IBuffer buffer = byteArray.AsBuffer();
    await FileIO.WriteBufferAsync(file, buffer);
}
Run Code Online (Sandbox Code Playgroud)

创建文件但文件为空.我究竟做错了什么?

And*_*pka 6

你为什么不使用FileIO.WriteBytesAsync方法?

public static IAsyncAction WriteBytesAsync(IStorageFile file, System.Byte[] buffer);
Run Code Online (Sandbox Code Playgroud)

您可以在一个行代码中执行此操作:

await FileIO.WriteBytesAsync(storageFile, byteArray);
Run Code Online (Sandbox Code Playgroud)