using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; // StreamReader namespace ankieta { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnMainWindowZapis_click(object sender, RoutedEventArgs e) { // otwieramy okno dialogowe formularza w celu pobrania ankiety WindowFormularz windowFormularz = new WindowFormularz(); windowFormularz.ShowDialog(); } // odczyt pliku ankieta.txt z bierzącego folderu projektu // https://docs.microsoft.com/pl-pl/dotnet/standard/io/how-to-read-text-from-a-file private async void btnMainWindowOdczyt_click(object sender, RoutedEventArgs e) { // obsługa wyjatków try { using (var sr = new StreamReader("ankieta.txt")) { // wypisujemy dane z pliku w TextBoxie tbDane.Text = await sr.ReadToEndAsync(); } } catch (FileNotFoundException ex) { // w przypadku błędu wypisujemy komunikat tbDane.Text = ex.Message; } } private void btnMainWindowUsun_click(object sender, RoutedEventArgs e) { // nazwa pliku do usuniecia string name = "ankieta.txt"; // pobieramy bieżący katalog projektu - WORKING directory (i.e. \bin\Debug) string workingDirectory = Environment.CurrentDirectory; if (File.Exists(System.IO.Path.Combine(workingDirectory, name))) { try { File.Delete(System.IO.Path.Combine(workingDirectory, name)); tbDane.Text = "Plik ankieta.txt został usunięty"; } catch (IOException ex) { // w przypadku błędu wypisujemy komunikat tbDane.Text = ex.Message; } } else { tbDane.Text = "Plik ankieta.txt nie istnieje"; } } } }