当前位置:首页 > 代码相关 > 正文内容

C# 使用FileStream进行文件复制操作

admin5年前 (2020-05-11)代码相关9257

使用文件流进行操作,如下,其中注释部分是和非注释部分一样的,但是使用using语句是执行完后自动释放内存,而注释部分是手动释放。

CopyFile方法中,缓冲区大小设为1024*1024字节,也就是1MB,Read方法和Write方法中,第一个参数都是缓冲区数组,第二个参数都是偏移量,这个量是在缓冲区中的偏移量,不要搞错,一般为0;第三个参数是要从缓冲区读取或写入的字节数。不同的是Read方法有返回值,为int类型,表示读取的有效字节数。两个方法在成功读取或写入后,再次读取或写入时,位置会移动到成功读取或写入字节后的位置,按我的理解应该是这个意思:第一次读了1M字节,再读的时候就从1M零一字节开始,依次类推。

class Program
    {
        static void Main(string[] args)
        {

            string sourcePath = @"K:视频前前前世 (movie ver.) RADWIMPS MV.mp4";
            string targetPath = @"C:UserscyhuDesktopcopy.mp4";
            Console.WriteLine("start!");
            CopyFile(sourcePath, targetPath);
            Console.WriteLine("complete!");

            Console.ReadKey();
        }

        public static void CopyFile(string sourcePath, string targetPath)
        {
            //FileStream fs = new FileStream(sourcePath, FileMode.OpenOrCreate, FileAccess.Read);
            //FileStream fsNew = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write);

            //byte[] buffer = new byte[1024 * 1024];

            //while (true)
            //{
            //    int len = fs.Read(buffer, 0, buffer.Length);
            //    if (len == 0)
            //    {
            //        break;
            //    }

            //    fsNew.Write(buffer, 0, len);
            //}

            //fsNew.Close();
            //fs.Close();

            using (FileStream fs = new FileStream(sourcePath,FileMode.OpenOrCreate,FileAccess.Read))
            {
                using (FileStream fsNew = new FileStream(targetPath,FileMode.OpenOrCreate,FileAccess.Write))
                {

                    byte[] buffer = new byte[1024 * 1024];

                    while (true)
                    {
                        int len = fs.Read(buffer, 0, buffer.Length);
                        if (len == 0)
                        {
                            break;
                        }

                        fsNew.Write(buffer, 0, len);
                    }
                }
            }
        }
    }


扫描二维码推送至手机访问。

版权声明:本文由lovedm.club发布,如需转载请注明出处。

本文链接:https://www.lovedm.club/?id=47

分享给朋友:

“C# 使用FileStream进行文件复制操作” 的相关文章

C# WPF程序 显示时间

C# WPF程序 显示时间

例子来源于此视频https://www.bilibili.com/video/av1422127?p=4 此例子是想说明有的类主要使用它的事件namespace Wpf_test {     /// <summary> &...

C#事件_Sample_1

事件模型的五个组成部分:1、事件的拥有者(event source,只能是对象或类)2、事件成员(event,成员)3、事件的响应者(event subscriber,对象)4、事件处理器(event handler,成员)--本质上是一个回调方法5、事件订阅--把事件...

C#事件_Sample_2

事件的拥有者与事件的响应者是分开的情况+=是事件订阅操作符,左边是事件,右边是事件处理器。using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者和事件的响应者是...

C#事件_Sample_3

事件的拥有者同时是事件的响应者using System; using System.Windows.Forms; /// <summary> /// 事件的拥有者同时是事件的响应者 /// </summary> na...

C# 泛型委托

C# 泛型委托

虽然没有必要,但是还是先看看自定义的泛型委托:namespace _20200402 {     class Program     {     &nb...

C# Lambda表达式

简单用法,一句一句来,便于理解 Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a * b; });(int a, int b) => { ret...