Working with files in a Windows Phone 7 application
While an application is running, it operates with various data. Some of it is temporary, that is only needed for a specific session, some of it is not – and it needs to be stored somewhere. A Windows Phone 7 application is generally capable of storing data in the isolated storage, being limited by the size quota assigned to it.
File operations on a Windows Phone 7 device are performed with the help of two classes: IsolatedStorageFileStream and IsolatedStorageFile. Via IsolatedStorageFileStream you are able to write and read data to file streams, while IsolatedStorageFile allows you to manage the files that are designated to the current application domain.
Let’s see an example that shows how a simple text file can be stored on the device.
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
string fileContents = "This is a sample text";
byte data =Encoding.UTF8.GetBytes(fileContents);
using (IsolatedStorageFileStream stream = file.CreateFile("file.txt"))
stream.Write(data, 0, data.Length);
The IsolatedStorageFile instance gets the storage unit assigned to the application (the quota assigned from the existing space). By default, when running in the context of the Windows Phone 7 emulator, the quota is set to 9,223,372,036,854,775,807– that is the positive limit for a signed 64-bit integer (long). On an actual device, expect this quota to be different.
The byte array contains the string that should be written to the file, in its byte representation. I used UTF8 encoding for it, but feel free to change it to Unicode if you need to.
Then there is an instance of IsolatedStorageFileStream that is set to a stream for a new file named file.txt. Then, the byte array is written to the file with the offset set to 0 and the length equal to the length of the array.
This will store the file in the root folder for the designated storage unit (for the specific application). But what if the user wants to write the file to a specific folder? In that case, the user has to create a folder first. This is done by calling the CreateDirectory method using the same IsolatedStorageFile instance that is already in use by the application.
In this case, you are also able to specify a relative path if you want to create a directory inside another directory. When saving files, you need to use the same relative path format to put a file inside a directory:
using (IsolatedStorageFileStream stream = file.CreateFile(@"test/file.txt"))
stream.Write(data, 0, data.Length);
When it comes to reading the file contents, you can use the same IsolatedStorageFileStream; however, instead of creating a file you will be instantiating IsolatedStorageFileStream to open a file for reading. As you can see, the same IsolatedStorageFile instance is used as a parameter:
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"test/file.txt",FileMode.Open,file))
long length = stream.Length;
byte decoded = new byte[length];
stream.Read(decoded, 0, (int)length);
Debug.WriteLine(Encoding.UTF8.GetString(decoded, 0, (int)length));
A byte array that will contain the file contents is instantiated with the length set to the length of the output stream. When the Read method is called for the stream, the byte array is populated with the data from the file and then I am decoding the string (since I know that I encoded it as UTF8) and displaying it in the Output dialog.
Deleting files is done by simply calling the DeleteFile method for the IsolatedStorageFile instance:
NOTE: Make sure you are using the correct path. Otherwise, an exception will be thrown.