作者:admin发布时间:2021-09-30分类:传奇私服浏览:78评论:23
场景
效果
将要批量复制的文件拖拽到窗体中然后点击下边选择目标文件夹然后点击复制按钮。
注
博客主页
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
上面是一个下面是和两个,然后添加一个路径选择控件。
(,) { listView1.GridLinestrue;//在各数据之间形成网格线 listView1.ViewView.Details;//显示列名称 listView1.FullRowSelecttrue;//在单击某项时对其进行选中 listView1.HeaderStyleColumnHeaderStyle.Nonclickable;//隐藏列标题 listView1.Columns.Add(文件路径,listView1.Width-5,HorizontalAlignment.Right); }
然后编写listView的脱拽事件使其能获取到拖拽文件并显示
privatevoidlistView1_DragEnter(objectsender,DragEventArgse) { e.EffectDragDropEffects.Copy; //设置拖放操作中目标放置类型为复制 String[]str_Drop(String[])e.Data.GetData(DataFormats.FileDrop,true);//检索数据格式相关联的数据 Data_List(listView1,str_Drop[0]); } publicvoidData_List(ListViewLV,stringF) //Form或MouseEventArgs添加命名空间usingSystem.Windows.Forms; { ListViewItemitemnewListViewItem(F); LV.Items.Add(item); }
然后编写三个点按钮的点击事件使其打开路径选择对话框并将选择的路径显示在TextBox中。
privatevoidbutton2_Click(objectsender,EventArgse) { if(folderBrowserDialog1.ShowDialog()DialogResult.OK) { textBox1.TextfolderBrowserDialog1.SelectedPath; } }
privatevoidbutton1_Click(objectsender,EventArgse) { stringFileName inttem_n0; stringDName if(textBox1.Text.Length0listView1.Items.Count0) { try { for(inti0;ilistView1.Items.Count;i) { FileNamelistView1.Items[i].SubItems[0].Text; tem_nFileName.LastIndexOf(\\ FileNameFileName.Substring(tem_n1,FileName.Length-tem_n-1); DNametextBox1.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
FileStreamFormerOpen; FileStreamToFileOpen; ///summary ///文件的复制 ////summary ///paramFormerFilestring源文件路径/param ///paramtoFilestring目的文件路径/param ///paramSectSizeint传输大小/param ///paramprogressBarProgressBarProgressBar控件/param publicvoidCopyFile(stringFormerFile,stringtoFile,intSectSize) { FileStreamfileToCreatenewFileStream(toFile,FileMode.Create); //创建目的文件如果已存在将被覆盖 fileToCreate.Close(); //关闭所有资源 fileToCreate.Dispose(); //释放所有资源 FormerOpennewFileStream(FormerFile,FileMode.Open,FileAccess.Read);//以只读方式打开源文件 ToFileOpennewFileStream(toFile,FileMode.Append,FileAccess.Write); //以写方式打开目的文件 //根据一次传输的大小计算传输的个数 //intmaxConvert.ToInt32(Math.Ceiling((double)FormerOpen.Length/(double)SectSize)); intFileSize; //要拷贝的文件的大小 //如果分段拷贝即每次拷贝内容小于文件总长度 if(SectSizeFormerOpen.Length) { byte[]buffernewbyte[SectSize]; //根据传输的大小定义一个字节数组 intcopied0; //记录传输的大小 while(copied((int)FormerOpen.Length-SectSize)) //拷贝主体部分 { FileSizeFormerOpen.Read(buffer,0,SectSize); //从0开始读每次最大读SectSize FormerOpen.Flush(); //清空缓存 ToFileOpen.Write(buffer,0,SectSize); //向目的文件写入字节 ToFileOpen.Flush(); //清空缓存 ToFileOpen.PositionFormerOpen.Position; //使源文件和目的文件流的位置相同 copiedFileSize; //记录已拷贝的大小 } intleft(int)FormerOpen.Length-copied; //获取剩余大小 FileSizeFormerOpen.Read(buffer,0,left); //读取剩余的字节 FormerOpen.Flush(); //清空缓存 ToFileOpen.Write(buffer,0,left); //写入剩余的部分 ToFileOpen.Flush(); //清空缓存 } //如果整体拷贝即每次拷贝内容大于文件总长度 else { byte[]buffernewbyte[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
已有23位网友发表了看法: