In this recipe, we will learn how to create files on the filesystem of iOS devices.
Create a new iPhone Single View Application in Xamarin Studio and name it FileCreationApp
.
Follow the ensuing steps to complete this recipe:
Open the
FileCreationAppViewController.xib
file in Interface Builder.Add a button and a label on its view.
Back in Xamarin Studio, enter the following code in the
ViewDidLoad
method of the controller class:string filePath = Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.Personal), "MyFile.txt"); using (StreamWriter sw = new StreamWriter (filePath)) { sw.WriteLine ("Some text in file!"); } this.btnShow.TouchUpInside += (s, e) => { using (StreamReader sr = new StreamReader (filePath)) { this.labelStatus.Text = sr.ReadToEnd (); } };
Compile and run the app on the simulator. Tap the button to fill the label with the contents of the file.