Visualizzazione post con etichetta IsolatedStorage. Mostra tutti i post
Visualizzazione post con etichetta IsolatedStorage. Mostra tutti i post

sabato 4 gennaio 2014

Windows Phone 8 - Folders and files


Windows Phone 8 introduced the ApplicationData class to easily work with IsolatedStorage and async/await keywords.

In Windows Phone currently it is possible to use only LocalFolder because RoamingStorage and TemporaryStorage throw the NotSupportedException.

However, all the operations can be done with a few lines.

How to use folders

Create folder

StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("myFolder", CreationCollisionOption.ReplaceExisting);


Read folder

var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");


Check if folder exists

StorageFolder folder;
try
{
    folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
    MessageBox.Show(folder.Name);
}
catch (FileNotFoundException ex)
{
    MessageBox.Show("Folder not found");
}


Read all folders

var folders = await ApplicationData.Current.LocalFolder.GetFoldersAsync();
foreach (var folder in folders)
    MessageBox.Show(folder.Name);


Rename folder

var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await folder.RenameAsync("myRenamedFolder");


Delete folder

var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await folder.DeleteAsync(StorageDeleteOption.PermanentDelete);


How to use files

Create file

var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);


Read all files

var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
foreach (var file in files)
    MessageBox.Show(file.Name);


Check if file exists

StorageFile file;
try
{
    file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
    MessageBox.Show(file.Name);
}
catch (FileNotFoundException ex)
{
    MessageBox.Show("file not found");
}

Write file

var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (var writer = new DataWriter(stream.GetOutputStreamAt(0)))
{
    writer.WriteString("Hello");
    await writer.StoreAsync();
    await writer.FlushAsync();
}


Read file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
var stream = await file.OpenAsync(FileAccessMode.Read);
 
using (var reader = new DataReader(stream.GetInputStreamAt(0)))
{
    var bytes = await reader.LoadAsync((uint)stream.Size);
    var s = reader.ReadString(bytes);
    MessageBox.Show(s);
}


Rename file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
await file.RenameAsync("myFileRenamed.txt");


Delete file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);


Copy file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await file.CopyAsync(folder);


Move file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await file.MoveAsync(folder);

The sample project

You can download the sample project here.


The original post is in the Microsoft TechNet Wiki

Mokapp 2013 - Thanks


Yes, we are in 2014.. and I'm really sorry for delay..

I want to thank all those who attended the Mokapp 2013 event.
A great day with great people.

Thanks to our sponsors and speakers.
And a special thanks to Syncfusion who gave free licenses to the new Windows Phone developers.


Demo and slides are online:
  • iOS7 - Multitasking e Dynamics - Daniele Galiotto - slide & demo
  • WP8 - Storage - Tiziano Cacioppolini - slide & demo
  • Android - Sviluppare con MVVM - Lorenzo Maiorfi - slide & demo
  • iOS7 - iBeacons e le novità di BLE - Francesco Novelli - slide & demo
  • WP8 - Imaging, applicazioni fotografiche - Fabrizio Bernabei - slide & demo
  • iOS7 WP8 Android - Side-by-side, sviluppo crossplatform - demo
See you at the next event! :D