Files and Folders in Windows Phone 8

Files and Folders

Files and folders support read and write operations using the StorageFolder class. In this article, we will see files and folders in Windows Phone 8 with the following features.

Write text to a text file (inside the folder)

Read the text file (inside the folder)

Delete the text file

Procedure

Create a new Windows Phone 8 Silverlight Project with a valid name. In my case, I will create a project with name "FilesAnFolder". Design your "MainPage.xaml" as shown below.

MainPage

Part A: writing some text into text file

Write the following code to get the data from the user and save it in the text file.
 
C# code

  1. private void savefilebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     string filename = filenametxtbox.Text;  
  4.     string userinput = userinputtxtbox.Text;  
  5.     if (filename == string.Empty)  
  6.     {  
  7.         MessageBox.Show("Enter File Name ");  
  8.     }  
  9.     else if (userinput == string.Empty)  
  10.     {  
  11.         MessageBox.Show("Enter User Input");  
  12.     }  
  13.     else  
  14.     {  
  15.         IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();  
  16.       using (StreamWriter write = new StreamWriter(new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, file)))  
  17.         {  
  18.            write.WriteLine(userinput);  
  19.            write.Close();  
  20.          }  
  21.          MessageBox.Show("File Saved ");  
  22.     }  

First we will check the file name and user input text to save the file and create an instance to get the file from the application. Using StreamWriter we will write the data in the text file.

Part B: Read text from text file

Now we will read the text from text file in the isolated storage root folder. Write the following code.

  1. private void readfilebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (filenametxtblock.Text == "")  
  4.     MessageBox.Show("Enter File name");  
  5.     else  
  6.     {  
  7.         string fileName = filenametxtbox.Text.ToString();  
  8.         IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();  
  9.         IsolatedStorageFileStream filestream = file.OpenFile(fileName, FileMode.Open, FileAccess.Read);  
  10.         using (StreamReader reader = new StreamReader(filestream))  
  11.         {  
  12.             string data = reader.ReadLine();  
  13.             MessageBox.Show(data.ToString());  
  14.         }  
  15.     }  

Part C: Delete the text file

Now we will delete the text file from isolated storage.

  1. private void deletefilebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (filenametxtbox.Text == "")  
  4.     {  
  5.     }  
  6.     else  
  7.     {  
  8.         string filename = filenametxtbox.Text;  
  9.         IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();  
  10.         file.DeleteFile(filename);  
  11.      }  

Part D: Writing some text into text file inside a folder
 
In this part, we will see how to create a folder and save the text file into that folder in Windows Phone 8 isolated storage. Write the code given below.

C# Code

  1. private void savefolderbtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (filenametxtbox.Text == "")  
  4.     {  
  5.        MessageBox.Show("Enter File name");  
  6.     }  
  7.     else if (foldernametxtbox.Text == "")  
  8.     {  
  9.         MessageBox.Show("Enter Folder name");  
  10.     }  
  11.     else if(userinputtxtbox.Text=="")  
  12.     {  
  13.        MessageBox.Show("Enter Data to save");  
  14.     }  
  15.     else  
  16.     {  
  17.         string filename = filenametxtbox.Text;  
  18.         string foldername = foldernametxtbox.Text;  
  19.         string data = userinputtxtbox.Text;  
  20.         IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();  
  21.         file.CreateDirectory(foldername);  
  22.         string filepath = foldername + "\\" + filename;  
  23.         StreamWriter write=new StreamWriter(new IsolatedStorageFileStream(filepath,FileMode.OpenOrCreate,file);  
  24.         write.WriteLine(data);  
  25.         write.Close();  
  26.         MessageBox.Show("File Saved");  
  27.     }  

Part E: Read the text file in folder

Next we will read the text file in a specific folder in isolated storage. Write the following code.

C# Code

  1. private void readfolderbtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (filenametxtbox.Text == "")  
  4.     {  
  5.        MessageBox.Show("Enter File name");  
  6.     }  
  7.     else if (foldernametxtbox.Text == "")  
  8.     {  
  9.        MessageBox.Show("Enter Folder name");  
  10.     }  
  11.     else  
  12.     {  
  13.         string filename = filenametxtbox.Text;  
  14.          string foldername = foldernametxtbox.Text;  
  15.          string filepath = foldername + "\\" + filename;  
  16.          IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();  
  17.          IsolatedStorageFileStream streamread=file.OpenFile(filepath,FileMode.Open,FileAccess.Read);  
  18.          using(StreamReader read=new StreamReader(streamread))  
  19.          {  
  20.             string data = read.ReadLine();  
  21.             MessageBox.Show(data);  
  22.          }
         }

Part F: Delete Folder/Directory

Our final part is to delete the folder or directory completely in isolated storage. Write the following code.

C# Code

  1. private void deletefolder_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (filenametxtbox.Text == "")  
  4.     {  
  5.        MessageBox.Show("Enter File name");  
  6.     }  
  7.     else  
  8.     {  
  9.        string filename = filenametxtbox.Text;  
  10.        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();  
  11.        file.DeleteDirectory(filename);  
  12.     }  

Now we can see the output as shown below.

output

Delete Folder


Similar Articles