一,参数类using System;namespace readfile { /// <summary> /// file_state 的摘要说明。 /// </summary> //参数类 public class file_state { public file_state() { // // TODO: 在此处添加构造函数逻辑 // } public string file_name; public long file_length; public byte [] buffer; public long buffer_length; public long pos; public System.Windows.Forms.ProgressBar progress; public System.Windows.Forms.TextBox textbox; } }
二、 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO;namespace readfile { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.OpenFileDialog openFileDialog1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.StatusBar statusBar1; public System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.SaveFileDialog saveFileDialog1; public static FileStream myfs=null; private string name; private FileStream fs_write=null;
{
/// <summary>
/// file_state 的摘要说明。
/// </summary>
//参数类
public class file_state
{ public file_state()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string file_name;
public long file_length;
public byte [] buffer;
public long buffer_length;
public long pos;
public System.Windows.Forms.ProgressBar progress;
public System.Windows.Forms.TextBox textbox;
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;namespace readfile
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.StatusBar statusBar1;
public System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.SaveFileDialog saveFileDialog1; public static FileStream myfs=null;
private string name;
private FileStream fs_write=null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255)));
this.button1.ForeColor = System.Drawing.Color.Red;
this.button1.Location = new System.Drawing.Point(240, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 24);
this.button1.TabIndex = 1;
this.button1.Text = "读 取 文 件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 78);
this.statusBar1.Name = "statusBar1";
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(576, 32);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = "statusBar1";
this.statusBar1.DrawItem += new System.Windows.Forms.StatusBarDrawItemEventHandler(this.statusBar1_DrawItem);
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Right;
this.progressBar1.Location = new System.Drawing.Point(232, 0);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(344, 78);
this.progressBar1.TabIndex = 3;
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox1.ForeColor = System.Drawing.Color.Red;
this.textBox1.Location = new System.Drawing.Point(368, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(72, 14);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "textBox1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(232, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 24);
this.button2.TabIndex = 5;
this.button2.Text = "保存的路径";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 48);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(200, 21);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "textBox2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(192)));
this.ClientSize = new System.Drawing.Size(576, 110);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Activated += new System.EventHandler(this.Form1_Activated);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.Filter="所有文件(*.*)|*.*";
this.openFileDialog1.CheckFileExists=true;
this.openFileDialog1.Title="选择文件";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.button1.Visible=false;
//////
//构造读filestream流
myfs=new FileStream(this.openFileDialog1.FileName,FileMode.Open,FileAccess.Read,FileShare.None,10240000);//缓冲区为10M /////////
string []box=this.openFileDialog1.FileName.Trim().Split('\\');
string a=box[box.Length-1];
int k=a.LastIndexOf(".");
string b=a.Substring(k+1,3);
this.textBox2.Text=this.textBox2.Text.Trim()+"."+b;
//初始化参数类
file_state my_file_state=new file_state();
my_file_state.pos=0;
my_file_state.file_name=this.openFileDialog1.FileName;
//获取文件长度
my_file_state.file_length=myfs.Length;
//缓冲区为1M
my_file_state.buffer_length=1024000;
my_file_state.progress=this.progressBar1;
my_file_state.textbox=this.textBox1;
my_file_state.progress.Maximum=(int)myfs.Length;
my_file_state.progress.Minimum=0;
my_file_state.buffer=new byte[(int)my_file_state.buffer_length];
//构造写文件流
//判断文件在不在
string path=this.textBox2.Text.Trim();
if(File.Exists(path))
File.Delete(path);//删除文件 fs_write=new FileStream(path,FileMode.Append,FileAccess.Write);
Application.DoEvents();
////构造回调方法
try
{
AsyncCallback mycallback=new AsyncCallback(dowork);
if( my_file_state.file_length>=my_file_state.buffer_length)
{
myfs.BeginRead(my_file_state.buffer,0,(int)my_file_state.buffer_length,mycallback,my_file_state);
}
else
{
myfs.BeginRead(my_file_state.buffer,0,(int)my_file_state.file_length,mycallback,my_file_state); }
}
catch(Exception y)
{
MessageBox.Show(y.Message.ToString());
}
}
}
public void dowork(IAsyncResult result)
{ file_state my_file_state=(file_state)result.AsyncState;
int byteread=myfs.EndRead(result);
Application.DoEvents();
//写文件
// lock(fs_write)
//{
fs_write.Write(my_file_state.buffer,0,byteread);
//}
Application.DoEvents(); my_file_state.pos+=byteread;
my_file_state.progress.Value+=byteread;
//检查文件有没有读完
if(my_file_state.pos<my_file_state.file_length)
{
long a=my_file_state.pos*100/myfs.Length;
Application.DoEvents();
my_file_state.textbox.Text="已完成"+a.ToString()+"%";
Application.DoEvents();
AsyncCallback mycallback=new AsyncCallback(dowork);
long left_size=my_file_state.file_length-my_file_state.pos;
if(left_size>=my_file_state.buffer_length)
{
myfs.BeginRead(my_file_state.buffer,0,(int)my_file_state.buffer_length,mycallback,my_file_state);
}
else
{
myfs.BeginRead(my_file_state.buffer,0,(int)left_size,mycallback,my_file_state);
}
}
else
{
Application.DoEvents();
my_file_state.textbox.Text="已完成100%";
MessageBox.Show(" 文件读取完毕!!大小为 "+(my_file_state.pos/1024.0).ToString("0.0")+"KB。","message");
Application.DoEvents();
my_file_state.textbox.Visible=false;
my_file_state.progress.Visible=false;
myfs.Close();
fs_write.Close();
Application.Exit(); }
} private void Form1_Load(object sender, System.EventArgs e)
{
this.textBox1.Text="";
this.statusBar1.ShowPanels=true;
this.statusBar1.Panels.Add(" 读 取 状 态:");
this.statusBar1.Panels[0].AutoSize=System.Windows.Forms.StatusBarPanelAutoSize.Contents;
this.statusBar1.Controls.Add(this.progressBar1);
this.button1.Enabled=false;
this.textBox2.Text="";
name="no_name";
} private void Form1_Activated(object sender, System.EventArgs e)
{
} private void statusBar1_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
} private void button2_Click(object sender, System.EventArgs e)
{
this.button1.Enabled=true;
this.saveFileDialog1.Title="设置路径";
this.saveFileDialog1.Filter="*.*|*.*";
this.saveFileDialog1.FileName=name;
if(this.saveFileDialog1.ShowDialog()==DialogResult.OK)
{
this.textBox2.Text=this.saveFileDialog1.FileName; }
}
}
}
用它的话如何写回掉函数?
用它的话如何写回掉函数?