Advertisement
Thewest123

22

Jun 14th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _180522_Černý1
  9. {
  10.     class Program
  11.     {
  12.         static string _soubor = "t";
  13.         static void Main(string[] args)
  14.         {
  15.             Console.ForegroundColor = ConsoleColor.Black;
  16.             Console.BackgroundColor = ConsoleColor.White;
  17.             while (true)
  18.             {
  19.                 Console.Clear();
  20.                 Console.Write($@"Menu:
  21. soubor: {_soubor}.txt
  22. k ~ konec
  23. fe ~ změň _soubor? BEZ KONCOVKY!!!!
  24. fc ~ vytvoř _soubor? BEZ KONCOVKY!!!!
  25. we ~ přidej text na konec
  26. ws ~ přidej!!!! text na začátek
  27. ra ~ vypiš celý obsah souboru
  28. rl ~ vypiš obsah souboru po řádcích
  29. rf ~ vypiš obsah souboru první řádek a zbytek
  30. Co chceš:");
  31.  
  32.                 string vstup = Console.ReadLine();
  33.  
  34.                 if (vstup == "k") break;
  35.                 if (vstup == "fe") ZmenSoubor();
  36.                 else if (vstup == "fc") VytvorSoubor();
  37.                 else if (vstup == "we") PridejTextNaKonec();
  38.                 else if (vstup == "ws") PridejTextNaZacatek();
  39.                 else if (vstup == "ra") VypisCelyObsah();
  40.                 else if (vstup == "rl") VypisObsahPoRadcich();
  41.                 else if (vstup == "rf") VypisPrvniRadekAZbytek();
  42.                 else Console.WriteLine("Neumím!");
  43.  
  44.                 Console.WriteLine("Odenteruj!");
  45.                 Console.ReadLine();
  46.             }
  47.         }
  48.  
  49.         private static void ZmenSoubor()
  50.         {
  51.             Console.Write("Zadej adresu souboru: ");
  52.             string vstup = Console.ReadLine();
  53.  
  54.             if (File.Exists(vstup + ".txt"))
  55.             {
  56.                 Console.WriteLine($"Soubor {vstup}.txt existuje!");
  57.                 _soubor = vstup;
  58.             }
  59.             else
  60.             {
  61.                 Console.WriteLine($"Soubor {vstup}.txt neexistuje!");
  62.             }
  63.         }
  64.  
  65.         private static void VytvorSoubor()
  66.         {
  67.             Console.Write("Zadej adresu souboru: ");
  68.             string vstup = Console.ReadLine();
  69.  
  70.             if (!File.Exists(vstup + ".txt"))
  71.             {
  72.                 Console.WriteLine($"Soubor {vstup}.txt NEexstiuje a je vytvořen!");
  73.                 File.Create(_soubor + ".txt");
  74.                 _soubor = vstup;
  75.             }
  76.             else
  77.             {
  78.                 Console.WriteLine($"Soubor {vstup}.txt již existuje!");
  79.             }
  80.         }
  81.  
  82.         private static void PridejTextNaKonec()
  83.         {
  84.             Console.Write("Zadej text: ");
  85.             string vstup = Console.ReadLine();
  86.  
  87.             var fs = new FileStream(_soubor + ".txt", FileMode.Append);
  88.             var sw = new StreamWriter(fs);
  89.  
  90.             sw.WriteLine(vstup);
  91.  
  92.             sw.Close();
  93.             fs.Close();
  94.         }
  95.  
  96.         private static void PridejTextNaZacatek()
  97.         {
  98.             Console.Write("Zadej text: ");
  99.             string vstup = Console.ReadLine();
  100.  
  101.             var fs = new FileStream(_soubor + ".txt", FileMode.Open);
  102.             var sr = new StreamReader(fs);
  103.             var sw = new StreamWriter(fs);
  104.  
  105.             string texToEnd = sr.ReadToEnd();
  106.             sw.WriteLine(vstup+texToEnd);
  107.  
  108.             sr.Close();
  109.             fs.Close();
  110.         }
  111.  
  112.         private static void VypisCelyObsah()
  113.         {
  114.             var fs = new FileStream(_soubor + ".txt", FileMode.Open);
  115.             var sr = new StreamReader(fs);
  116.  
  117.             Console.WriteLine(">>");
  118.             Console.ForegroundColor = ConsoleColor.Green;
  119.             Console.WriteLine(sr.ReadToEnd());
  120.             Console.ForegroundColor = ConsoleColor.Black;
  121.             Console.WriteLine("<<");
  122.  
  123.             sr.Close();
  124.             fs.Close();
  125.         }
  126.  
  127.         private static void VypisObsahPoRadcich()
  128.         {
  129.             var fs = new FileStream(_soubor + ".txt", FileMode.Open);
  130.             var sr = new StreamReader(fs);
  131.  
  132.             Console.WriteLine(">>");
  133.             Console.ForegroundColor = ConsoleColor.Green;
  134.  
  135.             int i = 1;
  136.             while (!sr.EndOfStream)
  137.             {
  138.                 Console.WriteLine($"{i} ~ {sr.ReadLine()}");
  139.                 i++;
  140.             }
  141.  
  142.             Console.ForegroundColor = ConsoleColor.Black;
  143.             Console.WriteLine("<<");
  144.  
  145.             sr.Close();
  146.             fs.Close();
  147.         }
  148.  
  149.         private static void VypisPrvniRadekAZbytek()
  150.         {
  151.             var fs = new FileStream(_soubor + ".txt", FileMode.Open);
  152.             var sr = new StreamReader(fs);
  153.  
  154.             Console.WriteLine(">>");
  155.             Console.ForegroundColor = ConsoleColor.Blue;
  156.             Console.WriteLine(sr.ReadLine());
  157.             Console.ForegroundColor = ConsoleColor.Green;
  158.             Console.WriteLine(sr.ReadToEnd());
  159.             Console.ForegroundColor = ConsoleColor.Black;
  160.             Console.WriteLine("<<");
  161.  
  162.             sr.Close();
  163.             fs.Close();
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement