Datei schreiben (erstellt und beschreibt eine Textdatei)

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace FileIO1
{

/// <summary>
/// Zusammenfassung für Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

private System.Windows.Forms.Button btn_work;
private System.Windows.Forms.Button btn_quit;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{

//
// Erforderlich für die Windows Form-Designerunterstützung
//
InitializeComponent();

//
// TODO: Fügen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu
//

}

/// <summary>
/// Die verwendeten Ressourcen bereinigen.
/// </summary>
protected override void Dispose( bool disposing )
{

if( disposing )
{

if (components != null)

{

components.Dispose();

}

}
base.Dispose( disposing );

}

#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.btn_work = new System.Windows.Forms.Button();
this.btn_quit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btn_work
//
this.btn_work.Location = new System.Drawing.Point(88, 16);
this.btn_work.Name = "btn_work";
this.btn_work.Size = new System.Drawing.Size(112, 24);
this.btn_work.TabIndex = 0;
this.btn_work.Text = "&Action !";
this.btn_work.Click += new System.EventHandler(this.btn_work_Click);
//
// btn_quit
//
this.btn_quit.Location = new System.Drawing.Point(88, 72);
this.btn_quit.Name = "btn_quit";
this.btn_quit.Size = new System.Drawing.Size(112, 24);
this.btn_quit.TabIndex = 1;
this.btn_quit.Text = "&Beenden";
this.btn_quit.Click += new System.EventHandler(this.btn_quit_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 118);
this.Controls.Add(this.btn_quit);
this.Controls.Add(this.btn_work);
this.Name = "Form1";
this.Text = "File I/O";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{

Application.Run(new Form1());

}

private void btn_quit_Click(object sender, System.EventArgs e)
{

this.Close();

}

private void btn_work_Click(object sender, System.EventArgs e)
{

SaveFileDialog createFileDialog1 = new SaveFileDialog();
createFileDialog1.Title = "Neue Datei auswählen";
createFileDialog1.Filter = "Textdatei (*.txt)|*.txt|beliebige Datei (*.*)|*.*";

if(createFileDialog1.ShowDialog() == DialogResult.OK)
{

System.IO.StreamWriter sw = new System.IO.StreamWriter(createFileDialog1.FileName);
//MessageBox.Show(sr.ReadToEnd()); für StreamReader() geeignet!
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
sw.Close();

}

}

}

}

Datei lesen (öffnet und liest eine Datei)

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace FileRead
{

/// <summary>
/// Zusammenfassung für Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

private System.Windows.Forms.Button btn_read;
private System.Windows.Forms.TextBox txt1;
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{

//
// Erforderlich für die Windows Form-Designerunterstützung
//
InitializeComponent();

//
// TODO: Fügen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu
//

}

/// <summary>
/// Die verwendeten Ressourcen bereinigen.
/// </summary>
protected override void Dispose( bool disposing )
{

if( disposing )
{

if (components != null)
{

components.Dispose();

}

}
base.Dispose( disposing );

}

#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{

this.btn_read = new System.Windows.Forms.Button();
this.txt1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btn_read
//
this.btn_read.Location = new System.Drawing.Point(96, 16);
this.btn_read.Name = "btn_read";
this.btn_read.Size = new System.Drawing.Size(96, 40);
this.btn_read.TabIndex = 0;
this.btn_read.Text = "Datei lesen...";
this.btn_read.Click += new System.EventHandler(this.btn_read_Click);
//
// txt1
//
this.txt1.Location = new System.Drawing.Point(16, 72);
this.txt1.Multiline = true;
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(264, 184);
this.txt1.TabIndex = 1;
this.txt1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.txt1);
this.Controls.Add(this.btn_read);
this.Name = "Form1";
this.Text = "Datei lesen";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{

Application.Run(new Form1());

}

private void btn_read_Click(object sender, System.EventArgs e)
{

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Datei auswählen";
openFileDialog1.Filter = "Textdatei (*.txt)|*.txt|beliebige Datei (*.*)|*.*";

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{

StreamReader sr = File.OpenText(openFileDialog1.FileName);
string zeile;
while (( zeile = sr.ReadLine() ) != null)
{

txt1.Text += zeile;
txt1.Text += "\r\n";

}
sr.Close();
btn_read.Enabled = false;

}

}

}

}

<<zurück>>