Cross Platform
Android
iOS

Save Documents

This recipe shows how to save a text file to the Documents folder.

Recipe

To save a string value to a text file

  • Ensure you reference System.IO in your code file:
using System.IO;
  • Determine the path for the Documents folder:
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
  • Construct a filename to save the document to:
var filename = Path.Combine (documents, "Write.txt");
  • Write to the file:
File.WriteAllText(filename, "Write this text into a file!");

Additional Information

iTunes File Access

Users can access files in the Documents folder via iTunes if you add the following key to your Info.plist file:

<key>UIFileSharingEnabled</key>  <true />

When the device is plugged in to iTunes, the Documents folder is accessible for users to upload or download files (they can also download directories, but they cannot “browse” directories via the iTunes user interface).

Read the File

To read the same text file from the Documents folder:

  • Determine the path for the Documents folder:
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
  • Construct the filename to read from:
var filename = Path.Combine (documents, "Write.txt");
  • Read the file’s contents into a variable:
var text = File.ReadAllText(filename);