Advertisement
jyoung12387

Read/Write to text file

Mar 9th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 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 MarchNine
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //text file path
  15.             string textFilePath = FilePath.GetFilePath();
  16.  
  17.             bool continueProgram;
  18.  
  19.             do
  20.             {
  21.                 continueProgram = true;
  22.                 StandardMessages.DetermineTask();
  23.                 string taskInput = Console.ReadLine();
  24.  
  25.                 if (taskInput.Equals("Add", StringComparison.OrdinalIgnoreCase))
  26.                 {
  27.                     //Get user input for the lines, then append them to the list.
  28.                     List<string> lines = DataEntry.UserInput();
  29.                     File.AppendAllLines(textFilePath, lines);
  30.                 }
  31.                 else if (taskInput.Equals("Read", StringComparison.OrdinalIgnoreCase))
  32.                 {
  33.                     ReadText.Read();
  34.                 }
  35.                 else if (taskInput.Equals("Exit", StringComparison.OrdinalIgnoreCase))
  36.                 {
  37.                     continueProgram = false;
  38.                 }
  39.                 else
  40.                 {
  41.                     StandardMessages.TaskError();
  42.                 }
  43.  
  44.             } while (continueProgram == true);
  45.  
  46.             StandardMessages.EndApplication();
  47.         }
  48.     }
  49. }
  50.  
  51. using System;
  52. using System.Collections.Generic;
  53. using System.Linq;
  54. using System.Text;
  55. using System.Threading.Tasks;
  56.  
  57. namespace MarchNine
  58. {
  59.     static class FilePath
  60.     {
  61.         public static string GetFilePath()
  62.         {
  63.             //static method that returns the specified file path
  64.             return @"C:\temp\monday string.txt";
  65.         }
  66.     }
  67. }
  68.  
  69. using System;
  70. using System.Collections.Generic;
  71. using System.Linq;
  72. using System.Text;
  73. using System.Threading.Tasks;
  74.  
  75. namespace MarchNine
  76. {
  77.     class DataEntry
  78.     {
  79.         //Method that takes user input, puts it in a list, and then retuns that list to the caller
  80.         public static List<string> UserInput()
  81.         {
  82.             List<string> output = new List<string>();
  83.  
  84.             bool addMoreEntries = true;
  85.  
  86.             do
  87.             {
  88.                 //Get user input from user
  89.                 Console.Write("Enter text to be stored: ");
  90.                 string readInput = Console.ReadLine();
  91.  
  92.                 //Get date to add to string
  93.                 DateTime date = DateTime.Now;
  94.  
  95.                 //Add user input to list
  96.                 output.Add($"{readInput}\t\t (written at {date : MM-dd-yyyy HH:mm:ss})" );
  97.  
  98.                 //Ask user to continue, if yes continue loop. if no exit.
  99.                 Console.WriteLine("Add more entries? (Y/N)");
  100.                 string addMoreEntriesInput = Console.ReadLine();
  101.  
  102.                 if (addMoreEntriesInput != "Y" && addMoreEntriesInput != "y")
  103.                 {
  104.                     addMoreEntries = false;
  105.                 }
  106.  
  107.             } while (addMoreEntries == true);
  108.  
  109.  
  110.             return output;
  111.  
  112.         }
  113.  
  114.     }
  115. }
  116.  
  117. using System;
  118. using System.Collections.Generic;
  119. using System.Linq;
  120. using System.Text;
  121. using System.Threading.Tasks;
  122. using System.IO;
  123.  
  124. namespace MarchNine
  125. {
  126.     class ReadText
  127.     {
  128.         public static void Read()
  129.         {
  130.             //Gets file path and then writes out content of the path to Console.
  131.             string textFilePath = FilePath.GetFilePath();
  132.  
  133.             string displayText = File.ReadAllText(textFilePath);
  134.             Console.WriteLine("Current entries:\n");
  135.             Console.WriteLine(displayText);
  136.         }
  137.     }
  138. }
  139.  
  140. using System;
  141. using System.Collections.Generic;
  142. using System.Linq;
  143. using System.Text;
  144. using System.Threading.Tasks;
  145.  
  146. namespace MarchNine
  147. {
  148.     class StandardMessages
  149.     {
  150.         public static void DetermineTask()
  151.         {
  152.             Console.WriteLine("What would you like to do?");
  153.             Console.WriteLine(@"Enter ""Add"" to add lines, ""Read"" to read lines, or ""Exit"" to exit program");
  154.         }
  155.  
  156.         public static void EndApplication()
  157.         {
  158.             Console.WriteLine("Program Complete");
  159.             Console.ReadLine();
  160.         }
  161.  
  162.         public static void TaskError()
  163.         {
  164.             Console.WriteLine("Entry not recognized. Please enter a correct task");
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement