wangoland

C# Tutorials : Writing to a text file using ‘StreamWriter'

Nov 8th, 2014
1,609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. // View the video tutorial here: https://www.youtube.com/watch?v=glyfSt5mh1Y
  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 Writing_to_a_Text_File
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             // Take 5 string inputs -> Store them in an array
  19.             // -> Write the array to a text file
  20.  
  21.             // Define our one and only variable
  22.             string[] names = new string[5]; // Array to hold the names
  23.  
  24.             Console.WriteLine("Enter 5 names to write to a text file: ");
  25.  
  26.             for (int i = 0; i < 5; i++)
  27.             {
  28.                 names[i] = Console.ReadLine();
  29.             }
  30.  
  31.             // Write this array to a text file
  32.  
  33.             StreamWriter SW = new StreamWriter(@"names.txt");
  34.  
  35.             for(int i = 0; i < 5; i++)
  36.             {
  37.                 SW.WriteLine(names[i]);
  38.             }
  39.  
  40.             SW.Close();
  41.         }
  42.     }
  43. }
Add Comment
Please, Sign In to add comment