Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Here is sample code to download and upload files to Azure Storage from Universal Windows Apps.
Complete source code is available here
Visual Studio template for Universal Windows Apps allows developers to build Windows Store App and Phone App in a single Visual Studio solution. This Visual Studio solution includes three projects, project for design and feature experiences unique to Store App, second project for Phone App and third project for shared code. For more details on Universal Apps template in Visual Studio please click here
Here is the Visual Studio projects for Universal Windows Apps.
The project ending with .Windows is for Store App, project ending with .WindowsPhone is for Phone App and .Shared is for shared code.
In the Shared Project, use following #defines to write specific code for Phone App or Store App
#if WINDOWS_PHONE_APP
// For Windows Phone App
#elif WINDOWS_APP
// For Windows Store App
#else
// Error
#endif
Azure Storage is storage in cloud which is accessible from anywhere in the world, from any type of application, whether it’s running in the cloud, on the desktop, on an on-premises server, or on a mobile or tablet device. Azure Storage support four types of storage : Blob Storage, Table Storage, Queue Storage and File Storage. For more details please click here.
Login to Azure Portal @ https://manage.windowsazure.com/
Click on New at the bottom left
Select Data Services | Storage | Quick Create and enter URL as shown below
Next create container, click on Create a Container in the Storage | UniversalAppAzureStorage | Container tab as shown below
Enter a name for container as shown below
Click on Manage Access Keys in the Dashboard tab and copy the Storage Account Name and Primary Access Key as shown
Open Visual Studio
Click on File | New
Select Templates | Visual C# | Store Apps | Universal Apps | Hub App as shown
Now install NuGet Package for Azure Store
Right click on the .Windows project and select Manage NuGet Packages
In the search window type Azure Storage as shown below
Click on Install button for Azure Storage
Now install the Azure Storage NuGet Package on .WindowsPhone project
Right click on the .WindowsPhone project and select Manage NuGet Packages
In the search window type Azure Storage and click on the install button
Now lets right code for uploading file to Azure Storage
Open App.xaml.cs file in the .Shared Project
Copy this below upload function
private async Task<int> UploadToAzureStorage()
{
try
{
// create Azure Storage
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=universalappazurestorage;AccountKey=<your key>");
// create a blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// create a container
CloudBlobContainer container = blobClient.GetContainerReference("containerone");
// create a block blob
CloudBlockBlob blockBlob = container.GetBlockBlobReference("filename");
// create a local file
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename", CreationCollisionOption.ReplaceExisting);
// copy some txt to local file
MemoryStream ms = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(string));
serializer.WriteObject(ms, "Hello Azure World!!");
using (Stream fileStream = await file.OpenStreamForWriteAsync())
{
ms.Seek(0, SeekOrigin.Begin);
await ms.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
// upload to Azure Storage
await blockBlob.UploadFromFileAsync(file);
return 1;
}
catch
{
// return error
return 0;
}
}
Now copy this below download function
private async Task<int> DownloadFromAzureStorage()
{
try
{
// create Azure Storage
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=universalappazurestorage;AccountKey=<your key>");
// create a blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// create a container
CloudBlobContainer container = blobClient.GetContainerReference("containerone");
// create a block blob
CloudBlockBlob blockBlob = container.GetBlockBlobReference("filename");
// create a local file
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename_from_azure", CreationCollisionOption.ReplaceExisting);
// download from Azure Storage
await blockBlob.DownloadToFileAsync(file);
return 1;
}
catch
{
// return error
return 0;
}
}
Now call these two functions
protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
#endif
int r = await UploadToAzureStorage();
if (r == 1)
{
await DownloadFromAzureStorage();
}
Frame rootFrame = Window.Current.Content as Frame;
Run the Windows Store App and check the files local storage
To get the path of the local storage, look at the Path variable value of the StorageFile as shown
here is the complete source code
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in