使用遍歷資料夾方法:這是我寫的複製方法,以下程式碼放到執行緒掛起來複製會比較順才不會佔cpu資源假宕機狀態。 Dim openname As String "要複製的路徑 Dim savenname As String "儲存的路徑 Dim Loadfilecount, Mincount As Integer "載入路徑複製檔案 Private Sub Loadfile() Mincount = 0 Loadfilecount = 0 Try For Each foundFile As String In My.Computer.FileSystem.GetFiles _ (openname) "遍歷當前目錄檔案 Mincount += 1 copyfile(foundFile) Next For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories _ (openname, FileIO.SearchOption.SearchAllSubDirectories) "遍歷所有資料夾 For Each foundFile As String In My.Computer.FileSystem.GetFiles _ (foundDirectory) "遍歷所有檔名稱 Mincount += 1 copyfile(foundFile) Next Next Catch ex As Exception MessageBox.Show(ex.Message) End Try MsgBox("複製完成!" & Loadfilecount.ToString & "個", , "提示!") End Sub""" <summary> """ 複製檔案 """ </summary> """ <param name="Cpname">檔案路徑和名稱</param> """ <remarks></remarks> Private Sub copyfile(ByVal Cpname As String) Dim Saname As String = "" Try Dim tname As String Dim Pathname As String Pathname = My.Computer.FileSystem.GetParentPath(Cpname).Replace(openname, "") "獲取資料夾名稱 tname = My.Computer.FileSystem.GetName(Cpname) Dim Newpathname As String Newpathname = savenname & Pathname If My.Computer.FileSystem.FileExists(Newpathname) = False Then "如果沒有資料夾則建立 My.Computer.FileSystem.CreateDirectory(Newpathname) End If Saname = savenname & Pathname & "\" & tname If My.Computer.FileSystem.FileExists(Saname) = True Then "如果檔案存在去掉只讀。 My.Computer.FileSystem.GetFileInfo(Saname).Attributes = 0 Try My.Computer.FileSystem.CopyFile(Cpname, Saname, True) Loadfilecount += 1 ListBox1.Items.Add(Cpname.Replace(openname, "..")) Catch ex As Exception MessageBox.Show("檔案未更新,請關閉相關程式後重試!" & vbCrLf & Cpname, "警告!") End Try Else My.Computer.FileSystem.CopyFile(Cpname, Saname, True) Loadfilecount += 1 End If Catch ex As Exception End Try End Sub
使用遍歷資料夾方法:這是我寫的複製方法,以下程式碼放到執行緒掛起來複製會比較順才不會佔cpu資源假宕機狀態。 Dim openname As String "要複製的路徑 Dim savenname As String "儲存的路徑 Dim Loadfilecount, Mincount As Integer "載入路徑複製檔案 Private Sub Loadfile() Mincount = 0 Loadfilecount = 0 Try For Each foundFile As String In My.Computer.FileSystem.GetFiles _ (openname) "遍歷當前目錄檔案 Mincount += 1 copyfile(foundFile) Next For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories _ (openname, FileIO.SearchOption.SearchAllSubDirectories) "遍歷所有資料夾 For Each foundFile As String In My.Computer.FileSystem.GetFiles _ (foundDirectory) "遍歷所有檔名稱 Mincount += 1 copyfile(foundFile) Next Next Catch ex As Exception MessageBox.Show(ex.Message) End Try MsgBox("複製完成!" & Loadfilecount.ToString & "個", , "提示!") End Sub""" <summary> """ 複製檔案 """ </summary> """ <param name="Cpname">檔案路徑和名稱</param> """ <remarks></remarks> Private Sub copyfile(ByVal Cpname As String) Dim Saname As String = "" Try Dim tname As String Dim Pathname As String Pathname = My.Computer.FileSystem.GetParentPath(Cpname).Replace(openname, "") "獲取資料夾名稱 tname = My.Computer.FileSystem.GetName(Cpname) Dim Newpathname As String Newpathname = savenname & Pathname If My.Computer.FileSystem.FileExists(Newpathname) = False Then "如果沒有資料夾則建立 My.Computer.FileSystem.CreateDirectory(Newpathname) End If Saname = savenname & Pathname & "\" & tname If My.Computer.FileSystem.FileExists(Saname) = True Then "如果檔案存在去掉只讀。 My.Computer.FileSystem.GetFileInfo(Saname).Attributes = 0 Try My.Computer.FileSystem.CopyFile(Cpname, Saname, True) Loadfilecount += 1 ListBox1.Items.Add(Cpname.Replace(openname, "..")) Catch ex As Exception MessageBox.Show("檔案未更新,請關閉相關程式後重試!" & vbCrLf & Cpname, "警告!") End Try Else My.Computer.FileSystem.CopyFile(Cpname, Saname, True) Loadfilecount += 1 End If Catch ex As Exception End Try End Sub