views:

877

answers:

3

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?).

+4  A: 

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.

Gonzalo
Thanks Gonzalo! I can't wait to try it.
Bryan
+3  A: 

The following How To from the MonoTouch user wiki web site has a few pointers to where to store your files:

http://wiki.monotouch.net/HowTo/Files/HowTo%3a%5FStore%5FFiles

miguel.de.icaza
+20  A: 

[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);
Console.WriteLine(text);

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 :)

Rory Blyth
Rory Rory Rory! Great explanation. That was absolutely perfect! Thanks so much!
Bryan
Glad to help :)
Rory Blyth
THANK YOU, I have been struggling with this as the simulator works with different write permissions it seems.
BahaiResearch.com