Thursday, November 1, 2012

File downloading and handling in Windows 8 Store Apps C# (RT)

With every new Microsoft platform comes a slightly different way to handle downloading and streaming files and Windows 8 is no exception.

I had to do a bit of research to figure out how to download and break open files so I thought I'd share my findings.

Downloading files to the local Storage:

//the location of the file to download
string myURL = "";

HttpClient client = new HttpClient { MaxResponseContentBufferSize = 100000 };

byte[] myData = await client.GetByteArrayAsync(myURL);

var localFolder = Windows.Storage.ApplicationData.Current.localFolder;

Storage file = await localFolder.CreateFileAsync("myfile.txt");

await FileIO.WriteBytesAsync(file, myData);

Reading a text from Local Storage:

var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

StorageFile file = await localFolder.GetFileAsync("myfile.txt");

string contents = await FileIO.ReadTextAsync(file);

Feel free to place questions in the comments


More Info:

No comments: