Download Content From Internet Location in Windows Store Apps

Today we will learn about the Background Transfer task in Windows Store Apps for downloading content from the Internet. The user can request files from the locations on the Internet. 

To access this feature you must include Background Transfer ability in your application. Windows Store apps provides an API for Background Transfer downloading called Background Transfer. Unlike the HTTP APIs, Background Transfer can be used by any language supported by the Windows Runtime.

In this article I will show you how to add Background Transfer Downloading feature in your Windows Store application Using C#/XAML. To download the file from the Internet here I use BackgroundDownloader Class.

Here are the instructions to be followed.

Step 1

Create a Blank Windows Store App using C#.

Step 2

To access the network in your application, you have to ensure that your app has the capability to access the internet. To add the capability use these steps:

  • In the Solution Explorer go to the Pacakage.appxmanifest.
  • Select the Capabilities tab from the top.
  • Check the Internet Checkbox and Picture Library.


Step 3

In this step I design the UI of the page using XAML.












            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                <GradientStop Color="Black"/>

                <GradientStop Color="White" Offset="1"/>



        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">       

            <StackPanel Orientation="Horizontal" Margin="0,10,0,0">

                <TextBlock Text="Local file name: " FontSize="25" Margin="0,8,17,0" />

                <TextBox x:Name="fileNameField" Text="SampleImage.png" Width="200"/>


            <StackPanel Orientation="Vertical" Margin="0,30,0,0">

                <Button x:Name="StartDownloadButton" Content="Start Download" Margin="80,0,10,0" Click="StartDownload_Click"/>

                <TextBlock x:Name="Msg" Margin="0,8,17,0" FontSize="20"  />

                <ProgressRing x:Name="Progress" Width="100" Height="50" Foreground="Red"></ProgressRing>





Step 4

Here is the coding of .cs file.

First of all add the namespaces to be used for the classes in the application they are:

using Windows.Networking.BackgroundTransfer;

using Windows.Storage;

Now, I create the source URI from which we want to download and the destination location where we want to store to; see:

Uri source;

,UriKind.Absolute, out source))



string destination = fileNameField.Text.Trim();         

StorageFile destinationFile = null; ;



    destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(

    destination, CreationCollisionOption.GenerateUniqueName);

catch (FileNotFoundException ex)



Here I created a new DownloadOperation object using the BackgroundDownloader class when passed the required StorageFile and the URI values that we created recently and call the CreateDownload method by passing two arguments; see:

BackgroundDownloader downloader = new BackgroundDownloader();

DownloadOperation download = downloader.CreateDownload(source, destinationFile);
await HandleDownloadAsync(download, true);

With the download configured, HandleDownloadAsync is called to attach progress and completion handlers to the operation before initiating the download; see:

private async Task HandleDownloadAsync(DownloadOperation download, bool start)




     if (start)


          // Start the download and attach a progress handler.

        await download.StartAsync();

    ResponseInformation response = download.GetResponseInformation();

    Msg.Text = "File is Successfully Downloaded";




   Msg.Text = "Problem Occurs in Doownloading";


Step 5

Now the app is ready for downloading content from the Internet. Press F5 and run it.

Click on the Start Download button.



Up Next
    Ebook Download
    View all
    View all