只读方式,不会影响其他程序对文件的操作

 

public static void CopyFile(string source, string target)
{
            //创建一个负责读取的流
            using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
            {
                //创建一个负责写入的流
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buffer = new byte[1024 * 1024 * 5];

                    //因为文件可能比较大所以在读取的时候应该用循坏去读取
                    while (true)
                    {
                        //返回本次实际读取到的字节数
                        int r = fsRead.Read(buffer, 0, buffer.Length);

                        if (r == 0)
                        {
                            break;
                        }
                        fsWrite.Write(buffer, 0, r);//写入
                    }

                }
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-06
  • 2021-10-18
  • 2022-01-19
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-18
  • 2022-12-23
  • 2021-06-28
  • 2021-12-11
  • 2022-12-23
  • 2022-02-02
  • 2021-09-21
相关资源
相似解决方案