Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating files


In this recipe, we will learn how to create files on the filesystem of iOS devices.

Getting ready

Create a new iPhone Single View Application in Xamarin Studio and name it FileCreationApp.

How to do it…

Follow the ensuing steps to complete this recipe:

  1. Open the FileCreationAppViewController.xib file in Interface Builder.

  2. Add a button and a label on its view.

  3. 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 ();
      }
    };
  4. Compile and run the app on the simulator. Tap the button to fill the label with the contents of the file.

How it works...

As one can see from the preceding code, we can use...