FileStream类操作文件

时间:2021-08-22 00:31:12

private void buttonselect_Click (object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择要复制的文件";
            ofd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
            ofd.Filter = "所有文件|*.*";
            ofd.ShowDialog();
            textBoxSelect.Text = ofd.FileName;
        }

private void buttonsave_Click (object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "请选择要保存文件的路径";
            sfd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
            sfd.Filter = "所有文件|*.*";
            sfd.ShowDialog();
            textBoxSave.Text = sfd.FileName;
            //先读取 再写入
            using (FileStream fsRead = new FileStream(textBoxSelect.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (FileStream fsWrite = new FileStream(textBoxSave.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Write))
                {
                    //设置进度条
                    progressBarFile.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);
                        progressBarFile.Value = (int)fsWrite.Length;
                    }

MessageBox.Show("保存成功");

}
            }

FileStream类操作文件