Ziomnexpl

Test_plikow

May 31st, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Test_plikow
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string path = @"plik.txt";//zmienna path zawiera ścieżkę
  11.             StreamWriter sw;          
  12.             if(!File.Exists(path))//sprawdza czy plik istnieje(! neguje odpowiedź)
  13.             {
  14.                 sw = File.CreateText(path);
  15.             }
  16.             else
  17.             {
  18.                 sw = new StreamWriter(path,true);
  19.             }
  20.             string tekst = Console.ReadLine();
  21.             sw.WriteLine(tekst);
  22.             sw.Close();
  23.             StreamReader sr = File.OpenText(path);
  24.             string odczyt = "";
  25.             int linia = 1;
  26.  
  27.             while ((odczyt = sr.ReadLine()) != null)
  28.             {
  29.                 Console.WriteLine(linia++ + ". " + odczyt);
  30.             }
  31.             sr.Close();
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment