SHFileOperationA 用法

本文介绍了一种利用Windows API中的SHFileOperationA函数实现文件复制的方法,并附带了VB.NET示例代码,该方法可在复制过程中显示进度条。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到有人复制文件带进度条,搜了一下发现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

转载于:https://www.cnblogs.com/LCX/archive/2011/05/29/2061447.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值