|
-
Dec 23rd, 2002, 01:38 PM
#1
Thread Starter
Fanatic Member
dtecting if a file is presently open
Greetings,
OK, here;'s the pseudocode. Are there command or properties to make it a reality?
' open a given file
' if the file is already opened by another user then
'MsgBox "Another user is presently using this file.
Data corruption may occur."
'end if
For example is there some file property I can evaluate to determine if the file is presently open?
Thank you,
Jim
-
Dec 23rd, 2002, 02:26 PM
#2
PowerPoster
just open the file. If you get an error #55, then it is already open and you will not be allowed to open it so "data corruption" is not an issue as you can't write to a file that someone else has open.
-
Dec 23rd, 2002, 02:49 PM
#3
Thread Starter
Fanatic Member
Originally posted by phinds
just open the file. If you get an error #55, then it is already open and you will not be allowed to open it so "data corruption" is not an issue as you can't write to a file that someone else has open.
What if they are using Terminal Services?
-
Dec 23rd, 2002, 05:01 PM
#4
Delete the file , and if you can delete it, then it's not open
-
Dec 23rd, 2002, 05:04 PM
#5
Actually, a safer way, would be to rename the file... and if you can rename it, then it's not open
-
Dec 23rd, 2002, 05:06 PM
#6
Frenzied Member
VB Code:
'Determine whether a file is already open or not
Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
Private Declare Function lClose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
Private Function IsFileAlreadyOpen(FileName As String) As Boolean
Dim hFile As Long
Dim lastErr As Long
' Initialize file handle and error variable.
hFile = -1
lastErr = 0
' Open for for read and exclusive sharing.
hFile = lOpen(FileName, &H10)
' If we couldn't open the file, get the last error.
If hFile = -1 Then
lastErr = Err.LastDllError
Else
' Make sure we close the file on success.
lClose (hFile)
End If
' Check for sharing violation error.
sFileAlreadyOpen = (hFile = -1) And (lastErr = 32)
End Function
Private Sub Form_Load()
MsgBox IsFileAlreadyOpen("c:\autoexec.bat")
End Sub
-
Dec 23rd, 2002, 05:09 PM
#7
I might be wrong, but if you open the file with this flag: FILE_SHARE_READ_WRITE, then it means that the file can be opened in the same time with another program ? So if that flag is used then the second Open will not return an error.
-
Dec 23rd, 2002, 05:36 PM
#8
JimMuglia
If you open the file exclusive no sharing, you will either open the file or get an error if it is already open.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|