Load a File

PDF for offline use
Sample Code:

Let us know how you feel about this

Translation Quality


0/250

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 BundleResource. 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, otherwise your app may fail when running on a physical device.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.