Save Documents

PDF for offline use:
Sample Code:
Related Articles:

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

Recipe

To save a string value to a text file

  1. Make sure you add a using statement for System.IO.
using System.IO;
  1. Determine the path for the **Documents** folder:
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
  1. Construct a filename to save the document to:
var filename = Path.Combine (documents, "Write.txt");
  1. 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:

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