將要批量複製的文件拖拽到窗體中,而後點擊下邊選擇目標文件夾,而後點擊複製按鈕。編程
注:數組
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。 緩存
新建一個窗體,佈局設計以下佈局
上面是一個ListView,下面是TextBox和兩個Button,而後添加一個路徑選擇控件。this
在窗體的load事件中對ListView進行樣式設置spa
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的脫拽事件,使其能獲取到拖拽文件並顯示.net
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; } }
而後編寫複製按鈕的點擊事件3d
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("文件複製錯誤。"); } } }
在複製按鈕的點擊事件中執行復制文件的方法CopyFilecode
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