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.
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
- private void savefilebtn_Click(object sender, RoutedEventArgs e)
- {
- string filename = filenametxtbox.Text;
- string userinput = userinputtxtbox.Text;
- if (filename == string.Empty)
- {
- MessageBox.Show("Enter File Name ");
- }
- else if (userinput == string.Empty)
- {
- MessageBox.Show("Enter User Input");
- }
- else
- {
- IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
- using (StreamWriter write = new StreamWriter(new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, file)))
- {
- write.WriteLine(userinput);
- write.Close();
- }
- MessageBox.Show("File Saved ");
- }
- }
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.
- private void readfilebtn_Click(object sender, RoutedEventArgs e)
- {
- if (filenametxtblock.Text == "")
- MessageBox.Show("Enter File name");
- else
- {
- string fileName = filenametxtbox.Text.ToString();
- IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
- IsolatedStorageFileStream filestream = file.OpenFile(fileName, FileMode.Open, FileAccess.Read);
- using (StreamReader reader = new StreamReader(filestream))
- {
- string data = reader.ReadLine();
- MessageBox.Show(data.ToString());
- }
- }
- }
Part C: Delete the text file
Now we will delete the text file from isolated storage.
- private void deletefilebtn_Click(object sender, RoutedEventArgs e)
- {
- if (filenametxtbox.Text == "")
- {
- }
- else
- {
- string filename = filenametxtbox.Text;
- IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
- file.DeleteFile(filename);
- }
- }
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
- private void savefolderbtn_Click(object sender, RoutedEventArgs e)
- {
- if (filenametxtbox.Text == "")
- {
- MessageBox.Show("Enter File name");
- }
- else if (foldernametxtbox.Text == "")
- {
- MessageBox.Show("Enter Folder name");
- }
- else if(userinputtxtbox.Text=="")
- {
- MessageBox.Show("Enter Data to save");
- }
- else
- {
- string filename = filenametxtbox.Text;
- string foldername = foldernametxtbox.Text;
- string data = userinputtxtbox.Text;
- IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
- file.CreateDirectory(foldername);
- string filepath = foldername + "\\" + filename;
- StreamWriter write=new StreamWriter(new IsolatedStorageFileStream(filepath,FileMode.OpenOrCreate,file);
- write.WriteLine(data);
- write.Close();
- MessageBox.Show("File Saved");
- }
- }
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
- private void readfolderbtn_Click(object sender, RoutedEventArgs e)
- {
- if (filenametxtbox.Text == "")
- {
- MessageBox.Show("Enter File name");
- }
- else if (foldernametxtbox.Text == "")
- {
- MessageBox.Show("Enter Folder name");
- }
- else
- {
- string filename = filenametxtbox.Text;
- string foldername = foldernametxtbox.Text;
- string filepath = foldername + "\\" + filename;
- IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
- IsolatedStorageFileStream streamread=file.OpenFile(filepath,FileMode.Open,FileAccess.Read);
- using(StreamReader read=new StreamReader(streamread))
- {
- string data = read.ReadLine();
- MessageBox.Show(data);
- }
}
}
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
- private void deletefolder_Click(object sender, RoutedEventArgs e)
- {
- if (filenametxtbox.Text == "")
- {
- MessageBox.Show("Enter File name");
- }
- else
- {
- string filename = filenametxtbox.Text;
- IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
- file.DeleteDirectory(filename);
- }
- }
Now we can see the output as shown below.