
界面.jpg

运行.jpg
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace CopyFileExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SelectButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择要复制的文件";
ofd.InitialDirectory = @"C:\Users\Sudo\桌面";
ofd.Filter = "所有文件|*.*";
ofd.ShowDialog();
SourceFileTextBox.Text = ofd.FileName;
}
private void SaveButton_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择要保存文件路径";
sfd.InitialDirectory = @"C:\Users\Sudo\桌面";
sfd.Filter = "所有文件|*.*";
sfd.ShowDialog();
DestFileTextBox.Text = sfd.FileName;
//先读取 再写入
using (FileStream fsRead = new FileStream(SourceFileTextBox.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Read))
{
using(FileStream fsWrite = new FileStream(DestFileTextBox.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Write))
{
//设置进度条最大值
SaveFileProgressBar.Maximum = (int)fsRead.Length;
//设置缓冲区大小
byte[] buffer = new byte[1024 * 1024 * 3];
while (true)
{
//读
int r = fsRead.Read(buffer, 0, buffer.Length);
if (r==0)
{
break;
}
//写
fsWrite.Write(buffer, 0, r);
//进度条进度值与写入数据大小关联
SaveFileProgressBar.Value =(int)fsWrite.Length;
}
MessageBox.Show("保存成功!");
}
}
}
}
}