下面是一個例子顯示如何拷貝文件:
1. 在Visual Basic中啓動一個新的EXE工程,其中包括Form1。
2. 添加兩個檢查框和一個按鈕在Form1上。
3. 加入以下代碼到Form1的代碼窗口:
Private Const FO_COPY = &H2& 'Copies the files specified
'in the pFrom member to the
'location specified in the
'pTo member.
Private Const FO_DELETE = &H3& 'Deletes the files specified
'in pFrom (pTo is ignored.)
Private Const FO_MOVE = &H1& 'Moves the files specified
'in pFrom to the location
'specified in pTo.
Private Const FO_RENAME = &H4& 'Renames the files
'specified in pFrom.
Private Const FOF_ALLOWUNDO = &H40& 'Preserve Undo information.
Private Const FOF_CONFIRMMOUSE = &H2& 'Not currently implemented.
Private Const FOF_CREATEPROGRESSDLG = &H0& 'handle to the parent
'window for the
'progress dialog box.
Private Const FOF_FILESONLY = &H80& 'Perform the operation
'on files only if a
'wildcard file name
'(*.*) is specified.
Private Const FOF_MULTIDESTFILES = &H1& 'The pTo member
'specifies multiple
'destination files (one
'for each source file)
'rather than one
'directory where all
'source files are
'to be deposited.
Private Const FOF_NOCONFIRMATION = &H10& 'Respond with Yes to
'All for any dialog box
'that is displayed.
Private Const FOF_NOCONFIRMMKDIR = &H200& 'Does not confirm the
'creation of a new
'directory if the
'operation requires one
'to be created.
Private Const FOF_RENAMEONCOLLISION = &H8& 'Give the file being
'operated on a new name
'in a move, copy, or
'rename operation if a
'file with the target
'name already exists.
Private Const FOF_SILENT = &H4& 'Does not display a
'progress dialog box.
Private Const FOF_SIMPLEPROGRESS = &H100& 'Displays a progress
'dialog box but does
'not show the
'file names.
Private Const FOF_WANTMAPPINGHANDLE = &H20&
'If FOF_RENAMEONCOLLISION is specified,
'the hNameMappings member will be filled
'in if any files were renamed.
' The SHFILOPSTRUCT is not double-word aligned. If no steps are
' taken, the last 3 variables will not be passed correctly. This
' has no impact unless the progress title needs to be changed.
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End Type
Private Declare Sub CopyMemory Lib "KERNEL32" _
Alias "RtlMoveMemory" _
(hpvDest As Any, _
hpvSource As Any, _
ByVal cbCopy As Long)
Private Declare Function SHFileOperation Lib "Shell32.dll" _
Alias "SHFileOperationA" _
(lpFileOp As Any) As Long
Private Sub Form_Load()
Check1.Caption = "Copy All Files in VB Directory"
Check2.Caption = "Display Custom Message"
Command1.Caption = "Copy Files"
End Sub
Private Sub Command1_Click()
Dim result As Long
Dim lenFileop As Long
Dim foBuf() As Byte
Dim fileop As SHFILEOPSTRUCT
lenFileop = LenB(fileop) ' double word alignment increase
ReDim foBuf(1 To lenFileop) ' the size of the structure.
With fileop
.hwnd = Me.hwnd
.wFunc = FO_COPY
' The files to copy separated by Nulls and terminated by two
' nulls
If Check1.Value = vbChecked Then
.pFrom = Environ("windir") & "*.exe"
.fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY
Else
.pFrom = Environ("windir") & "Explorer.exe" _
& vbNullChar _
& Environ("windir") & "WinHelp.exe" _
& vbNullChar _
& vbNullChar
End If
.pTo = "C: estfolder" & vbNullChar & vbNullChar
If Check2.Value = vbChecked Then
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or _
FOF_NOCONFIRMMKDIR
.lpszProgressTitle = "Your custom dialog string " & _
"appears here." & vbNullChar _
& vbNullChar
End If
End With
' Now we need to copy the structure into a byte array
Call CopyMemory(foBuf(1), fileop, lenFileop)
' Next we move the last 12 bytes by 2 to byte align the data
Call CopyMemory(foBuf(19), foBuf(21), 12)
result = SHFileOperation(foBuf(1))
If result <> 0 Then ' Operation failed
MsgBox Err.LastDllError 'Show the error returned from
'the API.
Else
If fileop.fAnyOperationsAborted <> 0 Then
MsgBox "Operation Failed"
End If
End If
End Sub
使用API函數SHFileOperation,這個函數可以同時拷貝、刪除、改名或移動多個文件,甚至整個目錄。如果你願意,還可以顯示相應的動畫對話框,功能十分強大。SHFileOperation的參數是一個SHFILEOPSSTRUCT結構。這個結構中各成員的含義如下:
- hwnd - 顯示文件操作對話框的窗口句柄
- wFunc - 表示要進行的操作,可以取以下值:
- FO_COPY - 拷貝文件。所要拷貝的文件由pFrom成員指定,目的地址有pTo成員指定。
- FO_DELETE - 刪除pFrom指定的文件。(pTo 被忽略。)
- FO_MOVE - 移動文件。所要移動的文件由pFrom成員指定,目的地址有pTo成員指定。
- FO_RENAME - 改名pFrom指定的文件。
- pFrom - 指定文件名的緩衝區的地址。必須以Chr(0)結尾。如果包括多個文件以Chr(0)分割。
- pTo - 指定目的文件名或目錄的緩衝區的地址。必須以Chr(0)結尾。如果使用了FOF_MULTIDESTFILES標誌,可以包括多個文件名,文件名之間以Chr(0)分割。
- fFlags - 標誌:
- FOF_ALLOWUNDO - 允許恢復
- FOF_FILESONLY - 如果使用了*.*,只操作文件。
- FOF_MULTIDESTFILES - pTo成員可以爲多個目的文件。
- FOF_NOCONFIRMATION - 不顯示確認對話框。
- FOF_NOCONFIRMMKDIR - 不確認是否建立目錄。
- FOF_NOERRORUI - 如果有錯誤,不顯示用戶界面。
- FOF_RENAMEONCOLLISION - 如果目的文件已經存在,給要處理的文件一個新名字。
- FOF_SILENT - 不顯示進度對話框。
- FOF_SIMPLEPROGRESS - 顯示進度框,但不顯示文件名。
- fAnyOperationsAborted -如果用戶退出,該成員爲TRUE,否則爲FALSE。
- lpszProgressTitle - 進度框的標題,只有選擇了FOF_SIMPLEPROGRESS標誌纔有效。