Take the 2-minute tour ×

How would I create and write to a file in a Monotouch iPhone app?

The file should persist between application launches, so I guess it has to be placed somewhere in the App bundle ( documents or resources?).

share|improve this question
add comment

3 Answers

up vote 95 down vote accepted

[Note: My response is pretty thorough because I don't know your level of understanding regarding app bundles or the structure of your iPhone app's sandboxed little world - apologies if I cover things you already know - I prefer to write a little too much than too little, and to add a bit of the why when discussing the how...]

You have a few options (of course). I'm assuming you're already familiar with .Net to some extent and that your question is more about how to do this the iPhone Way.

Every iPhone app (and you'll see the same thing for apps on OS X) is a "bundle" which isn't an executable in the traditional sense, but actually a folder hierarchy inside of which your app binary lives (along with resources, settings, etc.).

Because of how uber-sandboxed iPhone apps are, you don't have access to the shared folders you'd usually be able to use when doing desktop development (having, for example, a common Documents folder that lives under a user's home folder to which applications have access).

Instead, your app has its own folder hierarchy that's like its own personal set of the folders that would typically be shared across apps.

The easiest way to see what your app's folder structure looks like on the phone is to look at the folder the iPhone simulator uses for app installs, settings, blah blah blah. On my machine (I don't recall if this is configurable, but it's probably the same on your system), you can get to the folder by this path:

~/Library/Application Support/iPhone Simulator

Inside of that, there's a User/Applications folder that contains the apps you've installed to the simulator. Drill down into any one of those folders, and you can see the folder structure your app will have access to on the phone.

For storing files that you'd like persisted across app sessions, your app's Documents folder is the spot. It's not your only choice for creating files, but it's the right choice for this job. In addition to your files being properly stored, keeping them in the Documents folder will also get them backed up by iTunes when the user syncs.

With MonoTouch, you can get your app's Documents folder path with Environment.GetFolderPath(Environment.SpecialFolder.Personal);

If you'd like to test it out, this is some extremely simple code that'll write a file called "out.txt" to your app's Documents folder. This code also reads the contents of the file to show it was created - for further verification, go to the simulator's Applications folder, sort the app folders by the date they were modified, drill down into the most recently modified, and look inside its Documents folder - you'll find "out.txt" (you can't find your app's folder by name because, when your app is installed, it gets stuffed inside a folder with a name like "2B3CA854-FADB-4DDC-9732-0E61B3DD8D8C" - sorting the folders by the date they were modified will point you to the most recently modified app, which, in this case, is whatever app contains the following code):

// For this to function, don't forget "using System.IO;"

// If you're just playing around with this to see it work, place it inside
// your AppDelegate's "FinishedLaunching" method in main.cs

string path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, "out.txt");

// File.WriteAllText will create a file and then write text to it. If the
// file already exists, File.WriteAllText will overwrite it.
File.WriteAllText(filePath, "Howdy, world.");

// Now we prove it worked by reading the contents of the file and then
// printing them to the console...   
string text = File.ReadAllText(filePath);

So, the only thing here that's really iPhone-specific is knowing that "Environment.SpecialFolder.Personal" maps to your app's Documents folder. Beyond that, it's .Net as usual.

And, again, this was probably overkill, but I wanted to answer sufficiently thoroughly for everybody who sees it - hope this helps :)

share|improve this answer
Rory Rory Rory! Great explanation. That was absolutely perfect! Thanks so much! –  Bryan Dec 4 '09 at 21:49
Glad to help :) –  Rory Blyth Dec 5 '09 at 2:05
THANK YOU, I have been struggling with this as the simulator works with different write permissions it seems. –  BahaiResearch.com Feb 10 '10 at 11:48
After reading through answer I realized that it was answered back in '09 whereas in '12, it's still helpful. This is why I love SO.. Many thanks Rory, it helped to understand in detail. –  Subhash Dike Jan 17 '12 at 11:22
Keep in mind though that only application generated content should be stored in the Personal folder. Regenerable, reusable across sessions content should be stored in the Cache folder, and temporary (discardable between sessions) stuff in the temporary folder. I had may app rejected once for storing in the Personal folder content considered by the Apple reviewer as not generated by the app. –  Sorin Comanescu Jun 1 '12 at 9:28
add comment

The following How To from the Xamarin.iOS guide site has a few pointers to where to store your files:


share|improve this answer
add comment

You can do something like this at runtime:

using (StreamWriter writer = new StreamWriter (Path.Combine (Environment.SpecialFolders.Documents, "yourfilename.ext"))) { }

and that will create the file. To open it for reading, use the same Path.Combine() but with StreamReader.

share|improve this answer
Thanks Gonzalo! I can't wait to try it. –  Bryan Dec 2 '09 at 0:53
add comment

Your Answer


