Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace AssesToFile
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         { // Begin MAIN
  13.  
  14.             // Set up variables
  15.             string record;
  16.             string dir;
  17.             string fileName;
  18.             string wPath;
  19.             string ext;
  20.             char ReadRecord;
  21.  
  22.             // Create Write path
  23.             Console.WriteLine("Where would you like to save to?");
  24.             dir = Console.ReadLine(); // Define filepath --
  25.             Console.WriteLine("What would you like to call the file? (Don't include file extension)");
  26.             fileName = Console.ReadLine(); // Define filename --
  27.  
  28.             // Check for trailing slash in path
  29.             string sub = dir.Substring(dir.Length - 1);
  30.             if (sub != "\\")
  31.             { // Begin IF
  32.                 dir = dir + "\\"; // Add trailing slash if required --
  33.             } // END IF
  34.  
  35.             // Build the file path
  36.             ext = ".txt";
  37.             wPath = string.Format("{0}{1}{2}", dir, fileName, ext); // Concatenate vars --
  38.             StreamWriter sw = new StreamWriter(wPath);
  39.            
  40.             Console.WriteLine("Enter name to write to file: (Type \"exit\" to exit record input.)");
  41.             do
  42.             { // Begin DO
  43.                 record = Console.ReadLine();
  44.                 sw.WriteLine(record); // Write contents of ReadLine() to file --
  45.             } // End DO
  46.             while (record != "exit"); // Keep writing records until exit command given --
  47.  
  48.             sw.Close(); // Close writer connection --
  49.  
  50.             StreamReader sr = new StreamReader(wPath);
  51.             Console.WriteLine("Would you like to view records? Y/N");
  52.             ReadRecord = Convert.ToChar(Console.ReadLine());
  53.             if (ReadRecord == 'Y' || ReadRecord == 'y') // If user wants to read file --
  54.             { // Begin IF
  55.                 Console.Clear();
  56.                 Console.WriteLine("File contents:");
  57.                 Console.WriteLine(sr.ReadToEnd()); // Print file contents --
  58.                 Console.WriteLine("\nPress any key to exit..");
  59.                 Console.ReadKey(); // Wait for user input --
  60.             } // END IF
  61.             else
  62.                 Console.WriteLine("\nPress any key to exit..");
  63.             Console.ReadKey(); // Wait for user input --
  64.         } // End MAIN
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement