SHFileOperationA 用法

看到有人复制文件带进度条,搜了一下发现SHFileOperationA很简单就实现了

Public Class Form1

Private Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
Structure SHFILEOPSTRUCT
Dim hwnd As IntPtr '窗口句柄
Dim wFunc As Integer '执行的操作
Dim pFrom As String '原地点
Dim pTo As String '目标地点
Dim fFlags As Int32 '操作执行方式
Dim fAnyOperationsAborted As Integer '错误代码返回
Dim hNameMappings As Integer
Dim lpszProgressTitle As Integer 'String
End Structure
Private Const FO_MOVE As Integer = &H1
Private Const FO_COPY As Integer = &H2
Private Const FO_DELETE As Integer = &H3
Private Const FOF_ALLOWUNDO As Integer = &H40

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileOp As New SHFILEOPSTRUCT
Dim result As Integer
Try
With FileOp
.hwnd
= Me.Handle

.wFunc
= FO_COPY
.pFrom
= "F:\path\*.*" & Chr(0) & Chr(0) 'vbNullChar & vbNullChar
.pTo = "c:\test" & Chr(0) & Chr(0) 'vbNullChar & vbNullChar

.fFlags
= FOF_ALLOWUNDO
End With
result
= SHFileOperation(FileOp)
If result <> 0 Then ' Operation failed
If Err.LastDllError <> 0 Then
MsgBox(Err.LastDllError) ' Msgbox the error that occurred in the API.
End If
Else
If FileOp.fAnyOperationsAborted <> 0 Then
MsgBox("Operation Failed")
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try


End Sub
End Class
posted @ 2011-05-29 00:02  大力  阅读(754)  评论(0编辑  收藏  举报