場景 效果 將要批量複製的文件拖拽到窗體中,然後點擊下邊選擇目標文件夾,然後點擊複製按鈕。 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 新建一個窗體,佈局設計如下 上面 ...
場景
效果
將要批量複製的文件拖拽到窗體中,然後點擊下邊選擇目標文件夾,然後點擊複製按鈕。
註:
博客主頁:
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