主题:在利用缓冲区复制文件的时候,想要用进度条来指示复制的进度,该怎么做。
刚学C#,这个是用VS2005做的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnCopyFile_Click(object sender, EventArgs e)
{
if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtCopyFile.Text = this.openFileDialog1.FileName;
}
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
if(this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
txtSaveFile.Text = this.saveFileDialog1.FileName;
}
}
private void btnConfirm_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@txtCopyFile.Text.ToString(),FileMode.Open);
FileStream fs2 = new FileStream(@txtSaveFile.Text.ToString(),FileMode.Create);
byte[] temp = new byte[100];
BufferedStream bs = new BufferedStream(fs);
BufferedStream bs2 = new BufferedStream(fs2);
while(fs.Read(temp,0,temp.Length) > 0)
{
fs2.Write(temp,0,temp.Length);
fs2.Flush();
}
if(fs2.Read(temp,0,temp.Length) == 0)
{
MessageBox.Show("复制完成!","提示");
}
fs.Close();
fs2.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnCopyFile_Click(object sender, EventArgs e)
{
if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtCopyFile.Text = this.openFileDialog1.FileName;
}
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
if(this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
txtSaveFile.Text = this.saveFileDialog1.FileName;
}
}
private void btnConfirm_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@txtCopyFile.Text.ToString(),FileMode.Open);
FileStream fs2 = new FileStream(@txtSaveFile.Text.ToString(),FileMode.Create);
byte[] temp = new byte[100];
BufferedStream bs = new BufferedStream(fs);
BufferedStream bs2 = new BufferedStream(fs2);
while(fs.Read(temp,0,temp.Length) > 0)
{
fs2.Write(temp,0,temp.Length);
fs2.Flush();
}
if(fs2.Read(temp,0,temp.Length) == 0)
{
MessageBox.Show("复制完成!","提示");
}
fs.Close();
fs2.Close();
}
}
}