Advertisement
Idanref

C#| Reading and writing with text files.

Feb 27th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 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 User_name_with_text_stream
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string FILE_NAME = "user.txt"; // The file name.
  14.             string Username; // A variable that stores the user name.
  15.             string GetAction;
  16.             if (File.Exists(FILE_NAME)) // Check: If the file exists.
  17.             {
  18.                 TextReader read = new StreamReader(FILE_NAME);
  19.                 Username = read.ReadLine(); // Reading the username from the file.
  20.                 read.Close();
  21.                 Console.WriteLine("Hello " + Username);
  22.                 Console.Write("To change the username, please input 'Change'\nTo delete the data base, input 'Delete' or 'Erase' (Case Insensitive): ");
  23.                 GetAction = Console.ReadLine();
  24.                 if (GetAction == "Change" || GetAction == "change")
  25.                 {
  26.                     File.Delete(FILE_NAME);
  27.                     Console.Clear();
  28.                     Console.Write("Please enter your username: ");
  29.                     Username = Console.ReadLine();
  30.                     TextWriter write = new StreamWriter(FILE_NAME); // Creating the file.
  31.                     write.WriteLine(Username); // Writing to the file.
  32.                     write.Close();
  33.                     Console.Clear();
  34.                     Console.WriteLine("Account succesfully registered.\nYou may restart the program now.\n(Press 'Enter' to restart the program).");
  35.                 }
  36.                 else if (GetAction == "Delete" || GetAction == "delete" || GetAction == "Erase" || GetAction == "erase")
  37.                 {
  38.                     File.Delete(FILE_NAME);
  39.                     Console.WriteLine("Press 'Enter' to continue..");
  40.                 }
  41.                 Console.ReadKey();
  42.             }
  43.  
  44.             else // If the file doesn't exist.
  45.             {
  46.                 Console.Write("Please enter your username: ");
  47.                 Username = Console.ReadLine();
  48.                 TextWriter write = new StreamWriter(FILE_NAME); // Creating the file.
  49.                 write.WriteLine(Username); // Writing to the file.
  50.                 write.Close();
  51.                 Console.Clear();
  52.                 Console.WriteLine("Account succesfully registered.\nYou may restart the program now.\n(Press 'Enter' to restart the program).");
  53.                 Console.ReadKey();
  54.             }
  55.  
  56.         }                
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement