Advertisement
Kulas_Code20

WriteToTextFile

May 10th, 2022
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 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 TextFileIO
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string folder = @"C:\Parentfolder\Childfolder3\";
  15.             string filename = "Datafile.txt";
  16.             string fullPath = folder + filename;
  17.  
  18.             Console.WriteLine("Write File\n");
  19.             List<string> addData = new List<string>();
  20.             addData.Add("Lloyd Samuel,Garcia,Cebu City,lloydgarcia123@gail.com,09224542311");
  21.             File.WriteAllLines(fullPath, addData);
  22.             Console.WriteLine("All entries written\n");
  23.  
  24.             //Display
  25.             Console.WriteLine("Read File\n");
  26.            
  27.             List<string> list = File.ReadAllLines(fullPath).ToList();
  28.             List<PersonInfo> people = new List<PersonInfo>();
  29.             foreach (var line in list)
  30.             {
  31.                 string[] entries = line.Split(',');
  32.                 PersonInfo newPerson = new PersonInfo();
  33.                 newPerson.FirstName = entries[0];
  34.                 newPerson.LastName = entries[1];
  35.                 newPerson.Address = entries[2];
  36.                 newPerson.Email = entries[3];
  37.                 newPerson.Contact = entries[4];
  38.                 people.Add(newPerson);
  39.             }
  40.             foreach (var person in people)
  41.             {
  42.                 Console.WriteLine($"{ person.FirstName}, { person.LastName}, { person.Address}, { person.Email}, { person.Contact}");
  43.             }
  44.             Console.ReadLine();
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement