khi bạn dùng hàm FileCopy, VB sẽ làm giúp bạn tất cả mọi thứ. Như vậy, bạn sẽ không kiểm soát dc quá trình copy. Nếu file có dung lượng lớn thì có thể treo máy. Để copy file hiệu quả, bạn nên copy theo block. Ta chia file cần copy ra thành từng khối nhỏ (block). Code mẫu :
Code:
'Nho Reference thu vien Scripting Runtime truoc
'sSource la duong dan file nguon
'sDestination la duong dan file dich
public function CopyFile(byval sSource as string, byval sDestination as string)as boolean
Dim lBlockSize as long,lTotal as long
Dim fso as new FileSystemObject
Dim Content() as byte 'mang byte chua noi dung file
on error goto Cancel
lBlockSize =524288 'VD lay size block là 512KB
lTotal = fso.GetFile(sSource).Size
open sSource for binary as #1
open sDestination for binary as #2
do while lTotal>0
if lBlockSize>=lTotal then lBlockSize=lTotal
redim Content(lBlockSize-1)
get #1,,Content
put #1,,Content
loop
Close
CopyFile=True
msgbox "Copy thanh cong"
exit sub
Cancel:
msgbox "Loi copy file"
end sub
cơ bản là như vậy. bạn có thể phát triển thêm. Cái chỗ blocksize ko nhất thiết phải là 512KB. Bạn có thể viết một hàm lựa chọn blocksize sao cho phù hợp với từng kích cỡ file khác nhau.
Bookmarks