IO操作(文件/文件夹操作与读写)

点击查看代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformIOAndExcel
{
    public partial class FrmIIOStream : Form
    {
        public FrmIIOStream()
        {
            InitializeComponent();
        }

//文件文本读写 文本
        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"D:\NewImgs\Graphics绘图介绍.txt";
            string pathto = @"D:\NewImgs02\Graphics绘图介绍1.txt"; //没有问题
            //string path = @"D:\NewImgs\c#.pptx";
            //string pathto = @"D:\NewImgs02\c#.pptx";  //不行
            //string path = @"D:\NewImgs\1.mp4";
            //string pathto = @"D:\NewImgs02\1.mp4";
            //string path = @"D:\NewImgs\aaaa.jpg";
            //string pathto = @"D:\NewImgs02\aaaa1.jpg"; //不行
            char[] data = new char[1024];
            using (StreamReader dr = new StreamReader(path))
            {
                using (StreamWriter sw = new StreamWriter(pathto))
                {
                    while (true)
                    {
                        int rcount = dr.Read(data, 0, data.Length);
                        if (rcount == 0) break;
                        sw.Write(data, 0, rcount);
                    }

                }
            }


        }

        string path = @"D:\NewImgs\1.mp4";
        string pathto = @"D:\NewImgs02\1.mp4";
        int size = 1024 * 1024 * 5;
        byte[] bytes;
        FileStream fr;
        FileStream fw;
        /// <summary>
        /// 文件流读写 图片、ppt、文本、视频
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string path = @"D:\NewImgs\1.mp4";
            string pathto = @"D:\NewImgs02\1.mp4";
            bytes = new byte[size];
            using(fr=new FileStream(path,FileMode.Open,FileAccess.Read))
            {
                using(fw=new FileStream(pathto,FileMode.Create,FileAccess.Write))
                {
                    int count = fr.Read(bytes, 0, size);
                    while(count>0)
                    {
                        fw.Write(bytes, 0, count);
                        count = fr.Read(bytes, 0, size);
                    }
                }
            }

        }


        /// <summary>
        /// MemoryStream 内存流   中转   图片、ppt、文本、视频
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string path = @"D:\NewImgs\1.mp4";
            string pathto = @"D:\NewImgs02\1.mp4";
            bytes = new byte[size];
            MemoryStream ms = new MemoryStream(size);
            using (fr = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                int count = fr.Read(bytes, 0, size);
                while (count > 0)
                {
                    ms.Write(bytes, 0, count);
                    count = fr.Read(bytes, 0, size);
                }
            }
            //写入流
            using(fw=new FileStream(pathto,FileMode.Create,FileAccess.Write))
            {
                ms.WriteTo(fw);
            }
        }

        /// <summary>
        /// 缓冲流读写文件  图片、ppt、文本、视频
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string path = @"D:\NewImgs\1.mp4";
            string pathto = @"D:\NewImgs02\1.mp4";
            bytes = new byte[size];
            using (fr = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                BufferedStream br = new BufferedStream(fr);
                using (fw = new FileStream(pathto, FileMode.Create, FileAccess.Write))
                {
                    BufferedStream bw = new BufferedStream(fw);
                    int count = br.Read(bytes, 0, size);
                    while (count > 0)
                    {
                        bw.Write(bytes, 0, count);
                        count = br.Read(bytes, 0, size);
                    }
                    bw.Close();
                }
                br.Close();
            }

        }

        /// <summary>
        /// 二进制读写器  图片、ppt、文本、视频
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            string path = @"D:\NewImgs\1.mp4";
            string pathto = @"D:\NewImgs02\1.mp4";
            bytes = new byte[size];
            fr = new FileStream(path, FileMode.Open, FileAccess.Read);
            fw = new FileStream(pathto, FileMode.Create, FileAccess.Write);
            using(BinaryReader br=new BinaryReader(fr))
            {
                using(BinaryWriter bw=new BinaryWriter(fw))
                {
                    int count = br.Read(bytes, 0, size);
                    while (count > 0)
                    {
                        bw.Write(bytes, 0, count);
                        count = br.Read(bytes, 0, size);
                    }
                }
            }
            fr.Close();
            fw.Close();
        }

        /// <summary>
        /// 异步复制大文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void button6_Click(object sender, EventArgs e)
        {
            await ReadFileAsync02(path);
        }

        private async Task ReadFileAsync02(string pathFName)
        {
            Console.WriteLine($"异步读:{Thread.CurrentThread.ManagedThreadId}");
            bytes = new byte[size];
            using (fr = new FileStream(pathFName, FileMode.Open, FileAccess.Read, FileShare.Read, size, true))
            {
                using (fw = new FileStream(pathto, FileMode.Append, FileAccess.Write, FileShare.None, size, true))
                {
                    int count = 0;
                    count = await fr.ReadAsync(bytes, 0, bytes.Length);
                    while (count != 0)
                    {
                        await fw.WriteAsync(bytes, 0, count);
                        count = await fr.ReadAsync(bytes, 0, bytes.Length);
                    }
                }
            }
            Console.WriteLine($"异步复制完成!");
        }

    }
}

posted @ 2025-08-18 18:12  昔_舍  阅读(7)  评论(0)    收藏  举报