jyoung12387

Reading/Writing to text file

Feb 29th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace ConsoleUI
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string filePath = @"C:\Demos\Test.txt";
  15.  
  16.             List<Person> people = new List<Person>();
  17.  
  18.             List<string> lines = File.ReadAllLines(filePath).ToList();
  19.  
  20.             foreach(string line in lines)
  21.             {
  22.                 string[] entries = line.Split(',');
  23.  
  24.                 Person newPerson = new Person();
  25.  
  26.                 newPerson.FirstName = entries[0];
  27.                 newPerson.LastName = entries[1];
  28.                 newPerson.Email = entries[2];
  29.  
  30.                 people.Add(newPerson);
  31.             }
  32.  
  33.             foreach (var person in people)
  34.             {
  35.                 Console.WriteLine($"{person.FirstName} {person.LastName}: {person.Email}");
  36.             }
  37.  
  38.             Console.ReadLine();
  39.         }
  40.     }
  41. }
  42.  
  43. using System;
  44. using System.Collections.Generic;
  45. using System.Linq;
  46. using System.Text;
  47. using System.Threading.Tasks;
  48.  
  49. namespace ConsoleUI
  50. {
  51.     class Person
  52.     {
  53.         public string FirstName { get; set; }
  54.         public string LastName { get; set; }
  55.         public string Email { get; set; }
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment