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

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

admin6年前 (2020-05-11)代码相关9589

使用文件流进行操作,如下,其中注释部分是和非注释部分一样的,但是使用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://lovedm.club/?id=47

分享给朋友:

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

C# try-catch处理异常

使用try-catch进行异常处理,下面是两个小例子:两个例子中没有写finally语句finally的作用是无论有无异常,finally下的语句都会执行。//简单的处理异常namespace _20200323 {     class ...

C# 委托

C# 委托

Action和Func是.NET类库的内置委托,以简便使用。Func有17个重载还可以使用delegate关键字创建委托下面的代码展示了这三种使用委托的方法namespace _20200327 {     public delegat...

C#事件_Sample_2

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

C# Lambda表达式

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

C# 正则表达式(1)

C# 正则表达式(1)

用于匹配输入文本的模式string s = "this is a test!"; string res = Regex.Replace(s, "^",&nbs...

C# 测量运行时间

使用Stopwatch类进行运行时间的监测要使用 System.Diagnostics 命名空间方法表 4Reset()停止时间间隔测量,并将运行时间重置为零。Restart()停止时间间隔测量,将运行时间重置为零,然后开始测量运行时间。Start()开始或继续测量某个时间间隔的运行时间。...