3

所以我正在尝试在我开始学习的 Visual Basic 中做一些事情,但这还不够。我主要使用来自互联网的代码。现在我想将几个文件从第一个文件夹复制到第二个文件夹并覆盖现有文件,我想在进度条上查看进度(所有文件加起来大约 2GB)

已解决:我找到了一些程序的源代码并使用了一些部件来完成这项工作

4

1 回答 1

11

这是我最喜欢的做法……使用SHFileOperation API

该 API 将自动显示进度,如下面的屏幕截图所示。

这是一个例子。将此代码粘贴到模块中

Public Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Public Const FO_COPY = &H2
Public Const FOF_SIMPLEPROGRESS = &H100

Public 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 Long
End Type

Public Sub VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
    Dim op As SHFILEOPSTRUCT

    With op
        .wFunc = FO_COPY
        .pTo = strTarget
        .pFrom = strSource
        .fFlags = FOF_SIMPLEPROGRESS
    End With

    '~~> Perform operation
    SHFileOperation op
End Sub

然后像这样复制文件或文件夹

Private Sub Sample()
    '~~> Copy Files
    Call VBCopyFolder("C:\Sample.Avi", "C:\NewSample.Avi")

    '~~> Copy Folders
    Call VBCopyFolder("C:\Temp1", "C:\Temp2")
End Sub

截屏

在此处输入图像描述

于 2013-01-09T13:34:06.263 回答