我在Windows應用程序中創建基本文件複製操作。我注意到System.IO File.Copy
隨機複製文件。 有沒有辦法控制應該先複製哪些文件。例如,如果我們想從最小文件大小開始複製文件。或按字母順序,讓我們說開始複製文件的文件名[從[開始]]到Z,或通過數字順序與文件名[從] 1至100.使用.NET System.IO文件按順序複製文件。複製
我使用這個簡單的代碼來複制文件夾中的文件,但這會「隨機」複製文件。如下所示:
Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
Dim source as string = "c:\copyfiles"
Dim destination as string = "d:\backup"
Dim filepath as string = destination & "\"
For Each filename As String In Directory.GetFiles(source)
If File.Exists(filename) Then
Dim dFile As String = String.Empty
Dim dFilePath As String = String.Empty
dFile = Path.GetFileName(filename) 'get filenames from source
dFilePath = filepath & dFile 'concatenate filepath and filename
File.Copy(filename, dFilePath, True) 'copy files from "c:\copyfiles" folder to destination
End If
Next
MsgBox("Copy Successful", vbOKOnly, "Message")
End Sub
使用LINQ順序'目錄的結果。 GetFiles'你想如何訂購複製。 – crashmstr
由於'System.IO File.Copy'只爲每個調用複製一個文件,因此不能隨機進行。 – crashmstr
@crashmstr如果可以從最小到最大的文件大小進行復制。 –