读写,复制文件

namespace Homework
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string pathSource = string.Empty;
string pathMB = string.Empty;
private void btnOpenPath_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.ShowDialog();
pathSource = ofd.FileName;
txtSourcePath.Text = pathSource;
}

private void btnXz_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(pathSource))
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.CheckPathExists = true;
sfd.FileName = Path.GetFileNameWithoutExtension(pathSource);
sfd.DefaultExt = Path.GetExtension(pathSource);
sfd.ShowDialog();
pathMB = sfd.FileName;
txtTargetPath.Text = pathMB;
}
}

private void button1_Click(object sender, EventArgs e)
{
if (File.Exists(pathSource))
{
CopyTo();
// CopyTo(pathSource, pathMB);
}
}

private void CopyTo()
{
using (FileStream fsRead = new FileStream(pathSource, FileMode.Open))
{
using (FileStream fsWrite = new FileStream(pathMB, FileMode.Create))
{
while (true)
{
byte[] bytes = new byte[1024 * 50 * 1024];
int count = fsRead.Read(bytes, 0, bytes.Length);
fsWrite.Write(bytes, 0, count);
if (count <= 0)
{
MessageBox.Show("OK");
break;
}
}
}
}
}
}
}

原文地址:https://www.cnblogs.com/wrnsweet/p/6177888.html