Skip to content
Amal Hashim
All posts
Snippet

Windows 8 - Download a file

Download a file in a Windows Store app with BackgroundDownloader and async/await in C#.

1 min read#.NET#C##Windows

In a Windows Store app, BackgroundDownloader downloads a file to a StorageFile. This method returns true when the download succeeds.

csharp
async Task<bool> DownloadFile(Uri source, StorageFile destination)
{
    try
    {
        BackgroundDownloader downloader = new BackgroundDownloader();
        DownloadOperation download =
            downloader.CreateDownload(source, destination);
        await download.StartAsync();

        return true;
    }
    catch
    {
        return false;
    }
}

To show progress, add a ProgressBar to the UI and call StartAsync().AsTask(progress) with a progress handler.