Winform中实现批量文件复制(附代码下载)

时间:2019-12-12
本文章向大家介绍Winform中实现批量文件复制(附代码下载),主要包括Winform中实现批量文件复制(附代码下载)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

场景

效果

将要批量复制的文件拖拽到窗体中,然后点击下边选择目标文件夹,然后点击复制按钮。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建一个窗体,布局设计如下

上面是一个ListView,下面是TextBox和两个Button,然后添加一个路径选择控件。

在窗体的load事件中对ListView进行样式设置

 private void Form1_Load(object sender, EventArgs e)
        {
            listView1.GridLines = true;//在各数据之间形成网格线
            listView1.View = View.Details;//显示列名称
            listView1.FullRowSelect = true;//在单击某项时,对其进行选中
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;//隐藏列标题
            listView1.Columns.Add("文件路径", listView1.Width - 5, HorizontalAlignment.Right);
        }

然后编写listView的脱拽事件,使其能获取到拖拽文件并显示

private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;       //设置拖放操作中目标放置类型为复制
            String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);//检索数据格式相关联的数据
            Data_List(listView1, str_Drop[0]);
        }
  public void Data_List(ListView LV, string F)  //Form或MouseEventArgs添加命名空间using System.Windows.Forms;
        {
            ListViewItem item = new ListViewItem(F);
            LV.Items.Add(item);
        }

然后编写三个点按钮的点击事件,使其打开路径选择对话框,并将选择的路径显示在TextBox中。

private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

然后编写复制按钮的点击事件

private void button1_Click(object sender, EventArgs e)
        {
            string FileName = "";
            int tem_n = 0;
            string DName = "";
            if (textBox1.Text.Length > 0 && listView1.Items.Count > 0)
            {
                try
                {
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        FileName = listView1.Items[i].SubItems[0].Text;
                        tem_n = FileName.LastIndexOf("\\");
                        FileName = FileName.Substring(tem_n + 1, FileName.Length - tem_n - 1);
                        DName = textBox1.Text.Trim() + "\\" + FileName;
                        CopyFile(listView1.Items[i].SubItems[0].Text, DName, 1024);
                        this.Text = "复制:" + listView1.Items[i].SubItems[0].Text;
                    }
                    MessageBox.Show("文件批量复制完成。");
                }
                catch
                {
                    MessageBox.Show("文件复制错误。");
                }
            }
        }

在复制按钮的点击事件中执行复制文件的方法CopyFile

FileStream FormerOpen;
        FileStream ToFileOpen;
        /// <summary>
        /// 文件的复制
        /// </summary>
        /// <param FormerFile="string">源文件路径</param>
        /// <param toFile="string">目的文件路径</param> 
        /// <param SectSize="int">传输大小</param> 
        /// <param progressBar="ProgressBar">ProgressBar控件</param> 
        public void CopyFile(string FormerFile, string toFile, int SectSize)
        {
            FileStream fileToCreate = new FileStream(toFile, FileMode.Create);  //创建目的文件,如果已存在将被覆盖
            fileToCreate.Close();          //关闭所有资源
            fileToCreate.Dispose();          //释放所有资源
            FormerOpen = new FileStream(FormerFile, FileMode.Open, FileAccess.Read);//以只读方式打开源文件
            ToFileOpen = new FileStream(toFile, FileMode.Append, FileAccess.Write); //以写方式打开目的文件
            //根据一次传输的大小,计算传输的个数
            //int max = Convert.ToInt32(Math.Ceiling((double)FormerOpen.Length / (double)SectSize));

            int FileSize;            //要拷贝的文件的大小
            //如果分段拷贝,即每次拷贝内容小于文件总长度
            if (SectSize < FormerOpen.Length)
            {
                byte[] buffer = new byte[SectSize];       //根据传输的大小,定义一个字节数组
                int copied = 0;          //记录传输的大小
                while (copied <= ((int)FormerOpen.Length - SectSize))   //拷贝主体部分
                {
                    FileSize = FormerOpen.Read(buffer, 0, SectSize);   //从0开始读,每次最大读SectSize
                    FormerOpen.Flush();        //清空缓存
                    ToFileOpen.Write(buffer, 0, SectSize);     //向目的文件写入字节
                    ToFileOpen.Flush();         //清空缓存
                    ToFileOpen.Position = FormerOpen.Position;    //使源文件和目的文件流的位置相同
                    copied += FileSize;         //记录已拷贝的大小
                }
                int left = (int)FormerOpen.Length - copied;      //获取剩余大小
                FileSize = FormerOpen.Read(buffer, 0, left);     //读取剩余的字节
                FormerOpen.Flush();         //清空缓存
                ToFileOpen.Write(buffer, 0, left);       //写入剩余的部分
                ToFileOpen.Flush();         //清空缓存
            }
            //如果整体拷贝,即每次拷贝内容大于文件总长度
            else
            {
                byte[] buffer = new byte[FormerOpen.Length];    //获取文件的大小
                FormerOpen.Read(buffer, 0, (int)FormerOpen.Length);   //读取源文件的字节
                FormerOpen.Flush();         //清空缓存
                ToFileOpen.Write(buffer, 0, (int)FormerOpen.Length);   //写放字节
                ToFileOpen.Flush();         //清空缓存
            }
            FormerOpen.Close();          //释放所有资源
            ToFileOpen.Close();          //释放所有资源
        }

代码下载

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12028246

原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12029056.html