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
- StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("FolderName",CreationCollisionOption.ReplaceExisting);
Read Folder
- var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName ");
- Check if the Folder Exists
- try
- {
- StorageFolder folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName");
-
- }
- catch(FileNotFoundException ex)
- {
-
- }
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("FolderName");
- await folder.RenameAsync("RenamedFolder");
Delete Folder
- var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName");
- await folder.DeleteAsync(StorageDeleteOption.PermanentDelete);
Create File
- var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("FileName.txt", CreationCollisionOption.ReplaceExisting);
Write File
- var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("FileName.txt", CreationCollisionOption.ReplaceExisting);
- var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
- using (var writer = new DataWriter(stream.GetOutputStreamAt(0)))
- {
- writer.WriteString("Welcome to windows phone");
- await writer.StoreAsync();
- await writer.FlushAsync();
- }
Read File
- var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.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);
- }
Check if a file exists
- try
- {
- file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");
- MessageBox.Show(file.Name);
- }
- catch (FileNotFoundException ex)
- {
- MessageBox.Show("file not found");
- }
Read all File
- var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
- foreach (var file in files)
- {
- MessageBox.Show(file.Name);
- }
Rename File
- var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");
- await file.RenameAsync("FileRename.txt");
Delete File
- var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");
- await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
Copy File
- var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");
- var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName ");
- await file.CopyAsync(folder);
Move File
- var file = await ApplicationData.Current.LocalFolder.GetFileAsync("FileName.txt");
- var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("FolderName ");
- await file.MoveAsync(folder);
Note
To understand how to create files and folders refer to the
link.