Windows Phone 7, Android and iOS with Mono VI: File Access

Windows Phone 7, Android and iOS with Mono VI: File Access

Previous posts in this series:

Mono I: Getting Started
Mono II: Basic Navigation
Mono III: Shared Libraries
Mono IV: Webservices
Mono IV: Content and Resource Files

In the previous post we covered including files in your application that could be accessed at runtime. The flipside of this is being able to write to files, and as you may have expected there are some subtle differences between WP7 and the two Mono platforms.

Windows Phone 7

WP7 applications are limited to writing to files located within Isolated Storage. The following code illustrates creating the User Store and then opening a file.

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using(var file = store.OpenFile("test.txt", FileMode.Create,FileAccess.Write))
using(var strm = new StreamWriter(file))
{     strm.Write("Contents to write to the test file");
}

 

iOS

– Writing to files via MonoTouch is done via the Personal special folder, as per the following example

string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
string filePath = Path.Combine(path, "test.txt");
using (var file = File.Open(filePath, FileMode.Create, FileAccess.Write))
using (var strm = new StreamWriter(file))
{
strm.Write("Contents to write to the test file");
}

Android

Writing to files on MonoDroid should also be done via the Personal special folder, as per the following example

string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);  string filePath = Path.Combine(path, "test.txt");
using (var file = File.Open(filePath, FileMode.Create, FileAccess.Write))
using (var strm = new StreamWriter(file))
{     strm.Write("Contents to write to the test file");
}

Leave a Reply

Your email address will not be published. Required fields are marked *