如何使用VB6复制打开的文件? [英] How can I copy an open file using VB6?
问题描述
我有一个旧版 VB6 应用程序,可将文件附件上传到数据库 BLOB 字段.除非用户打开文件,否则它工作正常.
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.
我尝试创建文件的副本,然后上传该副本,但令我惊讶的是,每当您尝试复制用户打开的文件时,FileCopy 过程都会收到权限被拒绝"错误.
I tried creating a copy of the file, then uploading that copy, but to my surprise, the FileCopy procedure gets a "permission denied" error whenever you try to copy a file that is open by the user.
这让我很惊讶,因为您可以在 Windows 资源管理器打开时复制文件,而且我假设 FileCopy 方法使用与资源管理器相同的 API 调用.
This suprised me, because you can copy a file in Windows Explorer while it is open, and I was assuming that the FileCopy method used the same API call as explorer.
无论如何,我的问题是:如何在 VB6 中复制打开的文件?
Anyway, my question is: How can I copy an open file in VB6?
推荐答案
回答我自己的问题:
基于这篇文章,下面描述了对我有用的答案.
Based on this article, the answer that worked for me is described below.
1 - 将此声明添加到 VB 文件中:
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 - 为该函数创建一个小包装器,如下所示:
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 - 用这个替换我之前对 FileCopy 的调用:
3 - Replace my previous call to FileCopy with this:
CopyFileEvenIfOpen sourceFile, tempFile
这篇关于如何使用VB6复制打开的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!