DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Working with files in a Windows Phone 7 application

Denzel D. user avatar by
Denzel D.
·
Aug. 13, 10 · Interview
Like (0)
Save
Tweet
Share
30.63K Views

Join the DZone community and get the full member experience.

Join For Free

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.

file.CreateDirectory("test");

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:

file.DeleteFile(@"test/file.txt");

NOTE: Make sure you are using the correct path. Otherwise, an exception will be thrown.

Windows Phone application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Keep Your Application Secrets Secret
  • Accelerating Enterprise Software Delivery Through Automated Release Processes in Scaled Agile Framework (SAFe)
  • Running Databases on Kubernetes
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: