How to Use Folders and Files in Windows Phone 8

The following is the list of operations we will see in this article.

  • Create Folder
  • Read Folder
  • Check if the Folder Exists
  • Read all Folders
  • Rename Folder
  • Delete Folder
  • Create File
  • Write File
  • Read File
  • Check if the File Exists
  • Read all File
  • Rename File
  • Delete File
  • Copy File
  • Move File

Create Folder

  1. StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("FolderName",CreationCollisionOption.ReplaceExisting);  
Read Folder
  1. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName ");  
  2. Check if the Folder Exists  
  3. try  
  4. {  
  5.    StorageFolder folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName");  
  6.   
  7. }  
  8. catch(FileNotFoundException ex)  
  9. {  
  10.   
  11. }  
Read all Folders
  1. var folders = await ApplicationData.Current.LocalFolder.GetFoldersAsync();  
  2. foreach (var folder in folders)  
  3. {  
  4. MessageBox.Show(folder.Name);  
  5. }  
Rename Folder
  1. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName");  
  2. await folder.RenameAsync("RenamedFolder");  
Delete Folder
  1. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName");  
  2. await folder.DeleteAsync(StorageDeleteOption.PermanentDelete);  
Create File
  1. var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("FileName.txt", CreationCollisionOption.ReplaceExisting);  
Write File
  1. var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("FileName.txt", CreationCollisionOption.ReplaceExisting);  
  2. var stream = await file.OpenAsync(FileAccessMode.ReadWrite);  
  3. using (var writer = new DataWriter(stream.GetOutputStreamAt(0)))  
  4. {  
  5.    writer.WriteString("Welcome to windows phone");  
  6.    await writer.StoreAsync();  
  7.    await writer.FlushAsync();  
  8. }  
Read File
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");  
  2. var stream = await file.OpenAsync(FileAccessMode.Read);  
  3.   
  4. using (var reader = new DataReader(stream.GetInputStreamAt(0)))  
  5. {  
  6.    var bytes = await reader.LoadAsync((uint)stream.Size);  
  7.    var s = reader.ReadString(bytes);  
  8.    MessageBox.Show(s);  
  9. }  
Check if a file exists
  1. try  
  2. {  
  3.    file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");  
  4.    MessageBox.Show(file.Name);  
  5. }  
  6. catch (FileNotFoundException ex)  
  7. {  
  8.    MessageBox.Show("file not found");  
  9. }  
Read all File
  1. var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();  

  2. foreach (var file in files)  
  3. {  
  4.    MessageBox.Show(file.Name);  
  5. }  
Rename File
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");  
  2. await file.RenameAsync("FileRename.txt");  
Delete File
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");  
  2. await file.DeleteAsync(StorageDeleteOption.PermanentDelete);  
Copy File
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");  
  2. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName ");  
  3. await file.CopyAsync(folder);  
Move File
  1. var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");  
  2. var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName ");  
  3. await file.MoveAsync(folder);  
Note

To understand how to create files and folders refer to the link.