Tornseglare
Tornseglare

Reputation: 1197

Universal Windows - await FileIO.WriteBytesAsync() don't seem to await file to get written

I am trying something that should be straightforward, saving and then loading a file in UWP. (Universal Windows Platform) However, it is not always working.

Functions:

public async Task<bool> WriteFile(string fileName, byte[] byteArray){
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

        await FileIO.WriteBytesAsync(file, byteArray);
        return true;
}

public async Task<IBuffer> ReadFile(string fileName){
        StorageFolder folder = ApplicationData.Current.LocalFolder;
        StorageFile file = await folder.GetFileAsync(fileName);

        IBuffer buffer = await FileIO.ReadBufferAsync(file);

        return buffer;
}

Calling code:

byte[] fancyByteArray = { 0,1,2,3,4,5 };
Task<bool> t = WriteFile("Testfile", fancyByteArray);
Task<byte[]> task = ReadFile("Testfile");
IBuffer res = task.Result;

Well, this works sometimes. And that is the problem, because sometimes it does not work! Sometimes the file seem to be created or recreated, but no content. (Size on disk are zero bytes) Sometimes I get a file-does-not-exist error.

It is not a big file, 64 bytes, so it cannot be something related to "big file still being written" to disk.

I assume "await" means "wait until those darn bytes are written to disk", but maybe I get this wrong?! Maybe I should somehow check that the thing actually is finished writing to disk?

I am using Visual Studio Community 2017, latest everything as far as I know. Applications target version are set to "Windows 10 Anniversary Edition (10.0; Build 14393)".

Upvotes: 0

Views: 1028

Answers (1)

AVK
AVK

Reputation: 3923

Your await is inside the method but your actual calls are not awaited. So before the writing is completed, it will try to read the content of the file.

Instead await your ReadFile and WriteFile like below.

byte[] fancyByteArray = { 0, 1, 2, 3, 4, 5 };  
bool t = await WriteFile("Testfile", fancyByteArray);  
byte[] task = await ReadFile("Testfile");

And your WriteFile and ReadFile should be like below

public async Task<bool> WriteFile(string fileName, byte[] byteArray)  
{  
    StorageFolder folder = ApplicationData.Current.LocalFolder;  
    StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);  
    await FileIO.WriteBytesAsync(file, byteArray);  
    return true;  
}  

public async Task<byte[]> ReadFile(string fileName)  
{  
    StorageFolder folder = ApplicationData.Current.LocalFolder;  
    StorageFile file = await folder.GetFileAsync(fileName);  
    IBuffer buffer = await FileIO.ReadBufferAsync(file);  
    return buffer.ToArray();  
}

Update:

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    byte[] fancyByteArray = { 0, 1, 2, 3, 4, 5 };
    bool t = await WriteFile("Testfile", fancyByteArray);
    byte[] task = await ReadFile("Testfile");
}

Hope this helps.

Upvotes: 6

Related Questions