Code :
using System; using System.Windows.Forms; using System.IO; using System.Text; namespace SandBox { public partial class Form1 : Form { FileStream sr; Timer t; const int BUFFER_SIZE = 8192; public Form1() { InitializeComponent(); sr = new FileStream ("c:\\in\\test.txt", FileMode. Open, FileAccess. Read, FileShare. ReadWrite); t.Interval = 2000; t. Tick += new EventHandler (t_Tick ); this. FormClosing += new FormClosingEventHandler (Form1_FormClosing ); } private void button1_Click(object sender, EventArgs e) { if (!t.Enabled) { t.Start(); } else { sr.Seek(0, SeekOrigin.Begin); textBox1.Text = string.Empty; t.Stop(); } } private void t_Tick(object sender, EventArgs e) { byte[] buff = new byte[BUFFER_SIZE ]; int read = 0; while (true) { read = sr.Read(buff, 0, BUFFER_SIZE); if (read == 0) break; textBox1.Text += Encoding.Default.GetString(buff, 0, read); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { if (t.Enabled) t.Stop(); sr.Close(); } finally { sr.Dispose(); } } } }
|