C# FileStream 复制文件操作
使用 FileStream 进行复制文件操作。
using System;
using System.IO;
namespace FileStream复制文件
{
class Program
{
static void Main(string[] args)
{
string read = @"G:\再见歌.MP4";//要复制文件的路径
string write = @"G:\再见歌-副本.MP4";//要复制文件的副本
Copy(read, write);//调用方法
Console.ReadKey();
}
static void Copy(string read, string write)
{
using (FileStream fsRead = new FileStream(read, FileMode.OpenOrCreate, FileAccess.Read))//读文件
{
using (FileStream fsWrite = new FileStream(write, FileMode.OpenOrCreate, FileAccess.Write))//写文件
{
while (true)//循环
{
byte[] br = new byte[1024 * 1024 * 3];//字节数组,3M大小
int r = fsRead.Read(br, 0, br.Length);//从流中读取字节块并将该数据写入给定缓冲区中。
if (r == 0)//判断 返回值是否为0,为0 表示读取完成。
{
break;//退出 while循环
}
fsWrite.Write(br, 0, r);//写入 文件。
}
}
}
Console.WriteLine(read + " 复制成功!保存为:"+ write);//提示
}
}
}输出结果:
