Load a File
This recipe shows how to load a file that you have bundled with your Xamarin.iOS application.
To load a file that is bundled in a Xamarin.iOS application:
- Add the file to your Xamarin.iOS project and ensure the
Build Actionis 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
Console.WriteLine(text); txtView.Text = text;
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.