Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace AssesToFile
- {
- class Program
- {
- static void Main(string[] args)
- { // Begin MAIN
- // Set up variables
- string record;
- string dir;
- string fileName;
- string wPath;
- string ext;
- char ReadRecord;
- // Create Write path
- Console.WriteLine("Where would you like to save to?");
- dir = Console.ReadLine(); // Define filepath --
- Console.WriteLine("What would you like to call the file? (Don't include file extension)");
- fileName = Console.ReadLine(); // Define filename --
- // Check for trailing slash in path
- string sub = dir.Substring(dir.Length - 1);
- if (sub != "\\")
- { // Begin IF
- dir = dir + "\\"; // Add trailing slash if required --
- } // END IF
- // Build the file path
- ext = ".txt";
- wPath = string.Format("{0}{1}{2}", dir, fileName, ext); // Concatenate vars --
- StreamWriter sw = new StreamWriter(wPath);
- Console.WriteLine("Enter name to write to file: (Type \"exit\" to exit record input.)");
- do
- { // Begin DO
- record = Console.ReadLine();
- sw.WriteLine(record); // Write contents of ReadLine() to file --
- } // End DO
- while (record != "exit"); // Keep writing records until exit command given --
- sw.Close(); // Close writer connection --
- StreamReader sr = new StreamReader(wPath);
- Console.WriteLine("Would you like to view records? Y/N");
- ReadRecord = Convert.ToChar(Console.ReadLine());
- if (ReadRecord == 'Y' || ReadRecord == 'y') // If user wants to read file --
- { // Begin IF
- Console.Clear();
- Console.WriteLine("File contents:");
- Console.WriteLine(sr.ReadToEnd()); // Print file contents --
- Console.WriteLine("\nPress any key to exit..");
- Console.ReadKey(); // Wait for user input --
- } // END IF
- else
- Console.WriteLine("\nPress any key to exit..");
- Console.ReadKey(); // Wait for user input --
- } // End MAIN
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement