How can I copy an open file using VB6?

前端 未结 3 1111
我寻月下人不归
我寻月下人不归 2020-12-21 10:31

I have a legacy VB6 application that uploads file attachments to a database BLOB field. It works fine unless a user has the file open.

I tried creating a copy of th

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 10:57

    If you would like to do the same without using the api:

    Function SharedFilecopy(ByVal SourcePath As String, ByVal DestinationPath As String)

    Dim FF1 As Long, FF2 As Long
    Dim Index As Long
    Dim FileLength As Long
    Dim LeftOver As Long
    Dim NumBlocks As Long
    Dim filedata As String
    Dim ErrCount As Long
    On Error GoTo ErrorCopy
    '-------------
    'Copy the file
    '-------------
    Const BlockSize = 32767
    FF1 = FreeFile
    Open SourcePath$ For Binary Access Read As #FF1
    FF2 = FreeFile
    Open DestinationPath For Output As #FF2
    Close #FF2
    
    Open DestinationPath For Binary As #FF2
    
    Lock #FF1: Lock #FF2
    
    FileLength = LOF(FF1)
    NumBlocks = FileLength \ BlockSize
    LeftOver = FileLength Mod BlockSize
    
    filedata = String$(LeftOver, 32)
    
    Get #FF1, , filedata
    Put #FF2, , filedata
    filedata = ""
    filedata = String$(BlockSize, 32)
    
    For Index = 1 To NumBlocks
        Get #FF1, , filedata
        Put #FF2, , filedata
    Next Index
    Unlock #FF1: Unlock #FF2
    SharedFilecopy = True
    

    exitcopy:

    Close #FF1, #FF2
    

    Exit Function

    ErrorCopy: ErrCount = ErrCount + 1

    If ErrCount > 2000 Then

    SharedFilecopy = False
    
    Resume exitcopy
    

    Else

    Resume
    

    End If

    End Function

提交回复
热议问题