Android
iOS
 

Load a File

This recipe shows how to load a file that you have bundled with your Xamarin.iOS application.

Recipe

To load a file that is bundled in a Xamarin.iOS application:

  • Add the file to your Xamarin.iOS project and ensure the Build Action is set to Content. The sample code has a file called ReadMe.txt in the TestData folder.
  • Load the file’s contents into a variable using ReadAllText. Notice the path that is passed to the method is relative to the project’s root.
var text = System.IO.File.ReadAllText("TestData/ReadMe.txt");
  • Do something with the text, such as write to the Console or show in a UITextView:
Console.WriteLine(text);
txtView.Text = text;

Additional Information

Remember that the iOS file system is case-sensitive (although the simulator is not). Ensure that you type file and directory names correctly.