How To Read Text File In Xamarin iOS App

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Browse Code here.
 
Prerequisites
  • Xamarin Studio.
  • Xcode.
The steps given below are required to be followed in order to read a text file in Xamarin iOS, using Xamarin Studio.

Step 1

Go To Xamarin Studio.

Click New Solution—> select iOS—>select App--> Choose single View App. Afterwards, click Next.

 
 
Step 2

In this step, configure your app. Give the app a name (Ex:sample), Organization Identifier, and afterwards, click Next.
 
 

step 3

In this step, give your project a name (Ex: Sample) and solution name (Ex: Sample). Give path of your project and click Create.
 

Step 4
 
Subsequently, go to the solution. In there, get all the files and sources in your project. Now, select Main.storyboard and double click to open Main.storyboard page.
 
 

Step 5

After opening the Main.storyboard, you can design this page, as per your desire.
 
 

Step 6

Now, go to the Toolbox window and scroll down. You will see all the tools and controls. Just drag and drop the Button.

Next, you need to align the Button, using constraints.
 
 

Step 7

Now, go to the Properties window. Select the Button and give it a name (Ex:btnRead).
 
 

Step 8

You need to drag and drop the label. Next, you need to align the Label, using constraints.

 
 
Step 9

Now, go to the Properties window and select another Label (Ex:lblDisplay).

 

Step 10

In this step, go to the ViewController.cs page and add the namespace as given below.

ViewController.cs 
  1. using System.IO;  
  2. using System.Threading;  
 

Step 11

Now, go to the ViewController.cs page and write the follwoing code.

ViewController.cs
  1. namespace XamariniOSReadFile {  
  2.     public partial class ViewController: UIViewController {  
  3.         protected ViewController(IntPtr handle): base(handle) {  
  4.             // Note: this .ctor should not contain any initialization logic.  
  5.         }  
  6.         public override void ViewDidLoad() {  
  7.             base.ViewDidLoad();  
  8.             // Perform any additional setup after loading the view, typically from a nib.  
  9.         }  
  10.         private int ReadFile() {  
  11.             int count = 0;  
  12.             using(StreamReader reader = new StreamReader("/Users/DELPIN/Desktop/xamarin.txt")) {  
  13.                 string filecontent = reader.ReadToEnd();  
  14.                 count = filecontent.Length;  
  15.                 Thread.Sleep(5000);  
  16.             }  
  17.             return count;  
  18.         }  
  19.         partial void BtnRead_TouchUpInside(UIButton sender) {  
  20.             int count = ReadFile();  
  21.             lblDisplay.Text = count.ToString();  
  22.         }  
  23.         public override void DidReceiveMemoryWarning() {  
  24.             base.DidReceiveMemoryWarning();  
  25.             // Release any cached data, images, etc that aren't in use.  
  26.         }  
  27.     }  
  28. // This is just a sample script. Paste your real code (javascript or HTML) here.  
  29. if ('this_is' == /an_example/) {  
  30.     of_beautifier();  
  31. else {  
  32.     var a = b ? (c % d) : e[f];  
  33. }  


Step 12

Now, go to Run option and choose "Debug". From the list of iPhone and iPad simulators, you can choose any one simulator and run it.
 
 

Output

After a few seconds, the app will start running on your iPhone simulator.You will see your app working successfully.

 

You can click "Read File" button that will read how many words are there in your file.



That's it. I hope you liked this article.


Similar Articles