|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace Übungen_LF6_WinFormanwendung
|
|
|
|
|
{
|
|
|
|
|
public partial class csv_Übung : Form
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
string[] strWerte;
|
|
|
|
|
|
|
|
|
|
public csv_Übung()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button_ende_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button_dateiÖffnen_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
dateiLesen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAnzeige_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
foreach (string zeile in strWerte)
|
|
|
|
|
{
|
|
|
|
|
listBoxDaten.Items.Add(zeile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void dateiLesen()
|
|
|
|
|
{
|
|
|
|
|
string dateiname = "";
|
|
|
|
|
StreamReader sr;
|
|
|
|
|
string zeile;
|
|
|
|
|
|
|
|
|
|
//Filter für Dateiauswahl setzen
|
|
|
|
|
openFileDialog1.Filter = "CSV-Datei (*.csv)|*.csv|Alle Dateien (*.*)|*.*";
|
|
|
|
|
|
|
|
|
|
//Dateiauswahldialog
|
|
|
|
|
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
dateiname = openFileDialog1.FileName;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Keine Datei ausgewählt!", "Fehler");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//versuchen, die Datei zu öffnen, Fehlermeldung mit try-catch
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
sr = new StreamReader(dateiname);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//erste Zeile überlesen
|
|
|
|
|
zeile = sr.ReadLine();
|
|
|
|
|
textBox_Header.Text = zeile;
|
|
|
|
|
strWerte[0] = zeile;
|
|
|
|
|
|
|
|
|
|
//zählen der zeilen
|
|
|
|
|
for ( int anzahlZeilen = 1; !sr.EndOfStream;anzahlZeilen++)
|
|
|
|
|
{
|
|
|
|
|
//nächste Zeile
|
|
|
|
|
zeile = sr.ReadLine();
|
|
|
|
|
|
|
|
|
|
strWerte[anzahlZeilen] = zeile;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|