logancberrypie

C# DataBases Lesson 1

Apr 18th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 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 Databases
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //Initial variables
  15.             bool flag = true;
  16.             string name;
  17.  
  18.             //Reading from the file and putting into list seperated by '\n' so everyline
  19.             string filename = @"testdoc.txt";
  20.             string text = Readfile(filename);
  21.             List<string> Nameslist = new List<string>();
  22.             Nameslist = text.Split('\n').ToList();
  23.  
  24.             //Resets the file
  25.             WriteFile(filename, "");
  26.            
  27.             //Keeps the loop running until flag != true
  28.             while (flag)
  29.             {
  30.                 //'Q' is halt condition
  31.                 Console.WriteLine("Enter the entity or enter 'Q' to quit");
  32.                 name = Console.ReadLine();
  33.                 if (name == "Q")
  34.                 {
  35.                     //End decision
  36.                     Nameslist.Sort();
  37.                     for (int i = 0;i<Nameslist.Count();i++)
  38.                     {
  39.                         AppendFile(filename, Nameslist[i]);
  40.                     }
  41.                     flag = false;    
  42.                 } else
  43.                 {
  44.                     //Add to list devision
  45.                     Nameslist.Add(name);
  46.                 }
  47.  
  48.             }
  49.  
  50.         }
  51.  
  52.         static void AppendFile(string filename , string Text)
  53.         {
  54.             StreamWriter File = new StreamWriter(filename, true);
  55.             File.WriteLine("\n"+Text);
  56.             File.Close();
  57.         }
  58.         static void WriteFile(string filename, string Text)
  59.         {
  60.             StreamWriter File = new StreamWriter(filename);
  61.             File.Write(Text);
  62.             File.Close();
  63.         }
  64.  
  65.         static string Readfile(string filename)
  66.         {
  67.             StreamReader File = new StreamReader(filename);
  68.             string temp = File.ReadToEnd();
  69.             File.Close();
  70.             return (temp);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment