wangoland

C# Tutorials : Reading from a file using 'StreamReader'

Nov 10th, 2014
1,590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. // View the video tutorial here: https://www.youtube.com/watch?v=HkkcPIZtfxU
  2.  
  3. // © Dan Parker 2014 - wangoland@gmail.com
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.IO;
  11.  
  12. namespace Read_from_Text_File
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             // Define our one and only variable
  19.             string[] names = new string[5];
  20.  
  21.             // Read the text from the text file, and insert it into the array
  22.             StreamReader SR = new StreamReader(@"names.txt");
  23.  
  24.             for (int i = 0; i < 5; i++)
  25.             {
  26.                 names[i] = SR.ReadLine();
  27.             }
  28.  
  29.             // Close the text file, so other applications/processes can use it
  30.             SR.Close();
  31.  
  32.             // Write the array to the Console
  33.  
  34.             Console.WriteLine("Here are your 5 names from the text file: ");
  35.  
  36.             for(int i = 0; i < 5; i++)
  37.             {
  38.                 Console.WriteLine(names[i]); // Displays the line to the user
  39.             }
  40.  
  41.             // Pause the application so the user can read the information on the screen
  42.             Console.ReadLine();
  43.         }
  44.     }
  45. }
Add Comment
Please, Sign In to add comment