【发布时间】:2015-05-05 22:39:06
【问题描述】:
我正在尝试测试 .txt 或 .ini 文件是否已被任何人打开。我有几个版本的 IsFileOpen。这是直接取自http://www.cpearson.com/excel/ISFILEOPEN.ASPX
Option Explicit
Option Compare Text
Public Function isfileopen_test(FileName As String, _
Optional ResultOnBadFile As Variant) As Variant
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IsFileOpen_test
' This function determines whether a the file named by FileName is
' open by another process. The fuction returns True if the file is open
' or False if the file is not open. If the file named by FileName does
' not exist or if FileName is not a valid file name, the result returned
' if equal to the value of ResultOnBadFile if that parameter is provided.xd
' If ResultOnBadFile is not passed in, and FileName does not exist or
' is an invalid file name, the result is False.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim FileNum As Integer
Dim ErrNum As Integer
Dim V As Variant
On Error Resume Next
''''''''''''''''''''''''''''''''''''''''''''
' If we were passed in an empty string,
' there is no file to test so return FALSE.
''''''''''''''''''''''''''''''''''''''''''''
If VBA.Trim(FileName) = vbNullString Then
If IsMissing(ResultOnBadFile) = True Then
isfileopen_test = False
Else
isfileopen_test = ResultOnBadFile
End If
Exit Function
End If
''''''''''''''''''''''''''''''''''''''''''''
' if the file doesn't exist, it isn't open
' so get out now
''''''''''''''''''''''''''''''''''''''''''''
V = Dir(FileName, vbNormal)
If IsError(V) = True Then
' syntactically bad file name
If IsMissing(ResultOnBadFile) = True Then
isfileopen_test = False
Else
isfileopen_test = ResultOnBadFile
End If
Exit Function
ElseIf V = vbNullString Then
' file doesn't exist.
If IsMissing(ResultOnBadFile) = True Then
isfileopen_test = False
Else
isfileopen_test = ResultOnBadFile
End If
Exit Function
End If
FileNum = FreeFile()
'''''''''''''''''''''''''''''''''''''''
' Attempt to open the file and lock it.
'''''''''''''''''''''''''''''''''''''''
Err.Clear
Open FileName For Input Lock Read As #FileNum
ErrNum = Err.Number
''''''''''''''''''''
' Close the file.
''''''''''''''''''''
Close FileNum
On Error GoTo 0
''''''''''''''''''''''''''''''''''''''
' Check to see which error occurred.
''''''''''''''''''''''''''''''''''''''
Select Case ErrNum
Case 0
''''''''''''''''''''''''''''''''''''''''''''
' No error occurred.
' File is NOT already open by another user.
''''''''''''''''''''''''''''''''''''''''''''
isfileopen_test = False
Case 70
''''''''''''''''''''''''''''''''''''''''''''
' Error number for "Permission Denied."
' File is already opened by another user.
''''''''''''''''''''''''''''''''''''''''''''
isfileopen_test = True
Case Else
''''''''''''''''''''''''''''''''''''''''''''
' Another error occurred. Assume open.
''''''''''''''''''''''''''''''''''''''''''''
isfileopen_test = True
End Select
End Function
我需要通过 VBA 执行此操作。我无法让它适用于 .txt 或 .ini 文件。如何检查 txt 或 ini 文件是否已被网络上的某个人打开?
编辑:无论txt和ini文件是否打开,它都会返回false。
如果您愿意,我正在尝试在客户端网络上构建分布式计算系统。我以前从未这样做过,我希望让它尽可能简单,所以我正在考虑通过 txt 文件进行通信。 MSMQ 看起来不错,但看起来学习曲线很长。我已经阅读了 stackoverflow 上关于分布式计算的所有帖子。
【问题讨论】:
-
这个例子有什么问题?你是如何测试它的?
-
请扩展“不工作”。如果您的分布式计算系统的用户告诉您它“不工作”,您将如何解决?您是否收到错误或意外结果?对于初学者来说,该函数顶部有
On Error Resume Next,这是个坏消息。该函数肯定有很多关于“错误文件的结果”的复杂代码 -
@Comintern 我添加了一个编辑。
-
您确定“打开”文件的进程正在锁定它吗?大多数文本实用程序不会锁定文件,除非它们正在主动写入文件。也就是说,您是否考虑过内存映射文件?
-
从问题来看,听起来您正在寻找一种进程间通信的形式。内存映射文件的使用与共享内存类似。那天我在论坛上发布了一些(非常旧且不完整的)代码here。