How can I copy an open file using VB6?

前端 未结 3 1106
我寻月下人不归
我寻月下人不归 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条回答
  •  借酒劲吻你
    2020-12-21 11:01

    Answering my own question:

    Based on this article, the answer that worked for me is described below.

    1 - Add this declaration to the VB file:

    Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
          (ByVal lpExistingFileName As String, _
          ByVal lpNewFileName As String, _
          ByVal bFailIfExists As Long) As Long
    

    2 - Create a little wrapper for that function, like so:

    Sub CopyFileEvenIfOpen(SourceFile As String, DestFile As String)
      Dim Result As Long
       If Dir(SourceFile) = "" Then
         MsgBox Chr(34) & SourceFile & Chr(34) & " is not valid file name."
       Else
         Result = apiCopyFile(SourceFile, DestFile, False)
       End If
    End Sub
    

    3 - Replace my previous call to FileCopy with this:

    CopyFileEvenIfOpen sourceFile, tempFile
    

提交回复
热议问题