Derik Whittaker

Syndication

News


How to Create new IStorageFiles in WinRT (C#)

When building WinRT applications there will be the need to create IStorageFiles in the local storage area of your application.  In this post I was going to walk through how we can do this step by step.

 

Step 1: Get the StorageForlder object for your application

var storageFolder = ApplicationData.Current.LocalFolder;


Step 2: Crate the folder you want to put your file into

var userImagesFolder = await CreateFolder(storageFolder, "UserImages" );

// this is the contents of the CreateFolder method

private async Task<StorageFolder> CreateFolder(StorageFolder storageFolder, string folderName)
{
    StorageFolder folder = null;

    // we do the try w/ empty catch because the GetFolderAsync will throw if not found and we cannot do await inside a catch block
    try
    {
        folder = await storageFolder.GetFolderAsync(folderName);
    }
    catch { }

    if (folder == null)
    {
        folder = await storageFolder.CreateFolderAsync(folderName);
    }

    return folder;

}


Step 3: Create the file object

var myImage = await userImagesFolder.CreateFileAsync("FileName.png", CreationCollisionOption.ReplaceExisting);


Step 4: Write content to the file object

// buffer is a pointer of a memory stream of an image

await FileIO.WriteBufferAsync(newGuestSilhouetteImage, buffer);

All the code in one block

public async Task CreateLocalFile()
{
	var storageFolder = ApplicationData.Current.LocalFolder;
	var userImagesFolder = await CreateFolder(storageFolder, "UserImages" );
	var myImage = await userImagesFolder.CreateFileAsync("FileName.png", CreationCollisionOption.ReplaceExisting);
	await FileIO.WriteBufferAsync(newGuestSilhouetteImage, buffer);
}

private async Task<StorageFolder> CreateFolder(StorageFolder storageFolder, string folderName)
{
    StorageFolder folder = null;

    // we do the try w/ empty catch because the GetFolderAsync will throw if not found and we cannot do await inside a catch block
    try
    {
        folder = await storageFolder.GetFolderAsync(folderName);
    }
    catch { }

    if (folder == null)
    {
        folder = await storageFolder.CreateFolderAsync(folderName);
    }

    return folder;
}

As you can see creating storage files is not much work and is pretty straight forward.

Till next time,


Posted 07-04-2012 2:07 PM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Will Duff wrote re: How to Create new IStorageFiles in WinRT (C#)
on 07-10-2012 1:50 AM

You could do away with your CreateFolder function entirely by using the CreationCollisionOption overload for CreateFolderAsync instead:

var storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("UserImages", CreationCollisionOption.OpenIfExists);

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)