久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Winform中實(shí)現(xiàn)批量文件復(fù)制(附代碼下載)

 行者花雕 2020-04-29

場(chǎng)景

效果

將要批量復(fù)制的文件拖拽到窗體中,然后點(diǎn)擊下邊選擇目標(biāo)文件夾,然后點(diǎn)擊復(fù)制按鈕。

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書,、教程推送與免費(fèi)下載,。

實(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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約