Convert Storage File to Byte Array in Universal Windows apps

This async function converts a storage file to a byte array.

public static async Task<byte[]> GetBytesAsync(StorageFile file)
    byte[] fileBytes = null;
    if (file == null) return null;
    using (var stream = await file.OpenReadAsync())
        fileBytes = new byte[stream.Size];
        using (var reader = new DataReader(stream))
            await reader.LoadAsync((uint)stream.Size);
    return fileBytes;

It accepts a StorageFile as argument and returns a byte array for it.

Vivek Maskara

I am pursuing BTech in Software Engineering from Delhi Technological University. I develop apps, create websites and blog about my app experiences.

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding