IsolatedStorage: Folders and Files

Introduction

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

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

  1. StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("myFolder", CreationCollisionOption.ReplaceExisting);  
Read folder
  1. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");  
Check if folder exists
  1. StorageFolder folder;  
  2. try  
  3.    {  
  4.       folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");  
  5.       MessageBox.Show(folder.Name);  
  6.    }  
  7. catch (FileNotFoundException ex)  
  8.    {  
  9.       MessageBox.Show("Folder not found");  
  10.    }  
Read all folders
  1. var folders = await ApplicationData.Current.LocalFolder.GetFoldersAsync();  
  2. foreach (var folder in folders)  
  3.    MessageBox.Show(folder.Name);  
Rename folder
  1. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");  
  2. await folder.RenameAsync("myRenamedFolder");  
Delete folder
  1. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");  
  2. await folder.DeleteAsync(StorageDeleteOption.PermanentDelete);  
How to use files

Create file

  1. var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);  
Read all files
  1. var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();  
  2. foreach (var file in files)  
  3.    MessageBox.Show(file.Name);  
Check if file exists
  1. StorageFile file;  
  2. try  
  3.    {  
  4.       file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");  
  5.       MessageBox.Show(file.Name);  
  6.    }  
  7. catch (FileNotFoundException ex)  
  8.    {  
  9.       MessageBox.Show("file not found");  
  10.    }  
Write file
  1. var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);  
  2. var stream = await file.OpenAsync(FileAccessMode.ReadWrite);  
  3. using (var writer = new DataWriter(stream.GetOutputStreamAt(0)))  
  4.    {  
  5.       writer.WriteString("Hello");  
  6.       await writer.StoreAsync();  
  7.       await writer.FlushAsync();  
  8.    }  
Read file
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");  
  2. var stream = await file.OpenAsync(FileAccessMode.Read);  
  3. using (var reader = new DataReader(stream.GetInputStreamAt(0)))  
  4.    {  
  5.       var bytes = await reader.LoadAsync((uint)stream.Size);  
  6.       var s = reader.ReadString(bytes);  
  7.       MessageBox.Show(s);  
  8.    }  
Rename file
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");  
  2. await file.RenameAsync("myFileRenamed.txt");  
Delete file
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");  
  2. await file.DeleteAsync(StorageDeleteOption.PermanentDelete);  
Copy file
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");  
  2. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");  
  3. await file.CopyAsync(folder);  
Move file
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");  
  2. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");  
  3. await file.MoveAsync(folder);  
Personal Blog: Blend Unleashed.
Ebook Download
View all
Learn
View all