我有一个遗留的VB6应用程序,它将文件附件上传到数据库BLOB字段。除非用户打开文件,否则它可以正常工作。
我尝试创建该文件的副本,然后上传该副本,但令我惊讶的是,每当您尝试复制用户打开的文件时,FileCopy过程都会收到“权限被拒绝”错误。
这让我感到很惊讶,因为您可以在Windows资源管理器打开时复制文件,我假设FileCopy方法使用与资源管理器相同的API调用。
无论如何,我的问题是:如何在VB6中复制打开的文件?
答案 0 :(得分:5)
回答我自己的问题:
Based on this article,对我有用的答案如下所述。
1 - 将此声明添加到VB文件中:
Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long
2 - 为该函数创建一个小包装器,如下所示:
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替换为:
CopyFileEvenIfOpen sourceFile, tempFile
答案 1 :(得分:3)
如果你想在不使用api的情况下做同样的事情:
函数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
退出功能
ErrorCopy: ErrCount = ErrCount + 1
如果ErrCount> 2000然后
SharedFilecopy = False
Resume exitcopy
否则
Resume
结束如果
结束功能
答案 2 :(得分:1)
更短的解决方案:
1-项目 - >引用。检查“Microsoft Scripting Runtime”
2-使用此:
Dim fso As New FileSystemObject
fso.CopyFile file1, file2