場(chǎng)景效果將要批量復(fù)制的文件拖拽到窗體中,然后點(diǎn)擊下邊選擇目標(biāo)文件夾,然后點(diǎn)擊復(fù)制按鈕。 注: 博客主頁: 實(shí)現(xiàn)新建一個(gè)窗體,,布局設(shè)計(jì)如下 上面是一個(gè)ListView,,下面是TextBox和兩個(gè)Button,然后添加一個(gè)路徑選擇控件,。 在窗體的load事件中對(duì)ListView進(jìn)行樣式設(shè)置 private void Form1_Load(object sender, EventArgs e) { listView1.GridLines = true;//在各數(shù)據(jù)之間形成網(wǎng)格線listView1.View = View.Details;//顯示列名稱listView1.FullRowSelect = true;//在單擊某項(xiàng)時(shí),,對(duì)其進(jìn)行選中listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;//隱藏列標(biāo)題listView1.Columns.Add("文件路徑", listView1.Width - 5, HorizontalAlignment.Right); } 然后編寫listView的脫拽事件,,使其能獲取到拖拽文件并顯示 private void listView1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; //設(shè)置拖放操作中目標(biāo)放置類型為復(fù)制String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);//檢索數(shù)據(jù)格式相關(guān)聯(lián)的數(shù)據(jù)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); } 然后編寫三個(gè)點(diǎn)按鈕的點(diǎn)擊事件,使其打開路徑選擇對(duì)話框,并將選擇的路徑顯示在TextBox中,。 private void button2_Click(object sender, EventArgs e) {if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = folderBrowserDialog1.SelectedPath; } } 然后編寫復(fù)制按鈕的點(diǎn)擊事件 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 = "復(fù)制:" + listView1.Items[i].SubItems[0].Text; } MessageBox.Show("文件批量復(fù)制完成,。"); }catch{ MessageBox.Show("文件復(fù)制錯(cuò)誤。"); } } } 在復(fù)制按鈕的點(diǎn)擊事件中執(zhí)行復(fù)制文件的方法CopyFile FileStream FormerOpen; FileStream ToFileOpen;/// <summary>/// 文件的復(fù)制/// </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); //創(chuàng)建目的文件,,如果已存在將被覆蓋fileToCreate.Close(); //關(guān)閉所有資源fileToCreate.Dispose(); //釋放所有資源FormerOpen = new FileStream(FormerFile, FileMode.Open, FileAccess.Read);//以只讀方式打開源文件ToFileOpen = new FileStream(toFile, FileMode.Append, FileAccess.Write); //以寫方式打開目的文件//根據(jù)一次傳輸?shù)拇笮?,?jì)算傳輸?shù)膫€(gè)數(shù)//int max = Convert.ToInt32(Math.Ceiling((double)FormerOpen.Length / (double)SectSize));int FileSize; //要拷貝的文件的大小//如果分段拷貝,,即每次拷貝內(nèi)容小于文件總長度if (SectSize < FormerOpen.Length) {byte[] buffer = new byte[SectSize]; //根據(jù)傳輸?shù)拇笮。x一個(gè)字節(jié)數(shù)組int copied = 0; //記錄傳輸?shù)拇笮?/span>while (copied <= ((int)FormerOpen.Length - SectSize)) //拷貝主體部分 { FileSize = FormerOpen.Read(buffer, 0, SectSize); //從0開始讀,,每次最大讀SectSizeFormerOpen.Flush(); //清空緩存ToFileOpen.Write(buffer, 0, SectSize); //向目的文件寫入字節(jié)ToFileOpen.Flush(); //清空緩存ToFileOpen.Position = FormerOpen.Position; //使源文件和目的文件流的位置相同copied += FileSize; //記錄已拷貝的大小 }int left = (int)FormerOpen.Length - copied; //獲取剩余大小FileSize = FormerOpen.Read(buffer, 0, left); //讀取剩余的字節(jié)FormerOpen.Flush(); //清空緩存ToFileOpen.Write(buffer, 0, left); //寫入剩余的部分ToFileOpen.Flush(); //清空緩存 }//如果整體拷貝,,即每次拷貝內(nèi)容大于文件總長度else{byte[] buffer = new byte[FormerOpen.Length]; //獲取文件的大小FormerOpen.Read(buffer, 0, (int)FormerOpen.Length); //讀取源文件的字節(jié)FormerOpen.Flush(); //清空緩存ToFileOpen.Write(buffer, 0, (int)FormerOpen.Length); //寫放字節(jié)ToFileOpen.Flush(); //清空緩存 } FormerOpen.Close(); //釋放所有資源ToFileOpen.Close(); //釋放所有資源} 代碼下載https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12028246 |
|