هذا المثال يوضح كيفية التعامل مع Seek في قراءة الملفات
using System;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Seek
{
public partial class frmSeek : Form
{
public frmSeek()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
void Seek()
{
string result = null;
try {
// قراءة الملف
FileStream fs = File.OpenRead(Application.ExecutablePath);
// byte وضع محتوي الملف في مصفوفة من نوع
byte[] buffer = new byte[fs.Length];
// وضع اكبر قيمة لكائن التغير
trackSeek.Maximum = (int)fs.Length;
// تحديد بداية القراءة
fs.Seek((int)trackSeek.Value, SeekOrigin.Begin);
// trackSeek.Value قراءة الملف بدايتاً من مؤشر
fs.Read(buffer, 0, buffer.Length);
fs.Close();
// تفريغ المتغير
fs = null;
// يقوم بقراءة كل بايت علي حدة ثم يحولها الي المحارف العامة
foreach(byte b in buffer)
{
if(b >= 31 && b <= 127) {
result += Convert.ToChar(b).ToString();
} else {
result += '.';
}
}
textSeek.Text = result;
this.Text = "Seek : " + trackSeek.Value.ToString() + " From : " + trackSeek.Maximum;
} catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void trackSeek_ValueChanged(object sender, EventArgs e)
{
Seek();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new frmSeek());
}
private Button btnCancel;
private TextBox textSeek;
private NumericUpDown trackSeek;
private void InitializeComponent()
{
this.btnCancel = new Button();
this.textSeek = new TextBox();
this.trackSeek = new NumericUpDown();
((ISupportInitialize)(this.trackSeek)).BeginInit();
this.SuspendLayout();
/*
* btnCancel
*/
this.btnCancel.Location = new Point(536, 375);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new Size(75, 23);
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
/*
* textSeek
*/
this.textSeek.Location = new Point(12, 12);
this.textSeek.Multiline = true;
this.textSeek.Name = "textSeek";
this.textSeek.ScrollBars = ScrollBars.Both;
this.textSeek.Size = new Size(599, 355);
/*
* trackSeek
*/
this.trackSeek.Location = new Point(12, 373);
this.trackSeek.Name = "trackSeek";
this.trackSeek.Size = new Size(200, 20);
this.trackSeek.ValueChanged += new EventHandler(this.trackSeek_ValueChanged);
/*
* frmSeek
*/
this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(623, 420);
this.Controls.Add(this.trackSeek);
this.Controls.Add(this.textSeek);
this.Controls.Add(this.btnCancel);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
((ISupportInitialize)(this.trackSeek)).EndInit();
this.MaximizeBox = false;
this.Name = "frmSeek";
this.Text = "Seek";
}
}
}