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